Stevebin.net :: Stephen Viswaraj
Search
 
About me
Articles
Photo Gallery
fun
My College City
   JavaScript Articles  Feedback 
FLAMES : Find out Relationship
You:
Your Mate:
 

Screen Design & Code,
Steve...

Introduction

Angels of Love are shell shocked with this new invention. It seems that the FLAMES have the power to pronounce and prophesy the truth as accurately.

FLAMES is ISO Certified and also a registered under Acts of Love. :-)

What is FLAMES?

F - Friend
L - Love
A - Affectionate
M - Most Beloved
E - Enemy
S - Sister

How to find it?

  1. Take Guy, and Girl Name.

ü       Example: Guy : Ajith ; Girl : Shalini

  1. Strike out all common letters between these two names.

ü       Remaining Words: jt and Slni

  1. Now, Count the remaining letters from both the words.

ü       6

  1. Now take the word FLAMES, calculate from the F and strike it on 6th letter S.

ü       FLAME

  1. Now again calculate from the next letter, keep on calculate and strike it out until to reach out only one letter remaining from the word FLAMES which decide your relationship.

ü       M

Well. Applying this algorithm by manually would take more time na. :-)

Source Code:

<script language="javascript">
var strGame = "FLAMES";
var positionIndex = -1;
var removableCharIndex = -1;

// --- Main Logic Starts --- 
function doMatch(frmObject)
{
	var strName1 = trimSpace(frmObject.Name1.value).toUpperCase();
	var strName2 = trimSpace(frmObject.Name2.value).toUpperCase();
	
	if(validateInputValues(strName1, strName2, frmObject) == false)
		return false;
	
	var existName1 = strName1;
	
	var i = 0;
	while(existName1.charAt(i))
	{
		if(doExist(strName2, existName1.charAt(i)) >= 0)
		{
			strName2 = doRemoveChar(strName2, existName1.charAt(i));
			strName1 = doRemoveChar(strName1, existName1.charAt(i));
		}
		i++;
	}

	var MatchlessCount = strName1.length + strName2.length;
	var resultWord = FindLastAlphabet(MatchlessCount);

	alert("\n" + repeatChar('*', 65) + "\n" + 
		"You : \t" + frmObject.Name1.value.toUpperCase() + "\n" 
		+ "Your Mate : \t" + frmObject.Name2.value.toUpperCase() + "\n" 
		+ "Relationship :" + repeatChar('-', 10) + resultWord 
		+ repeatChar('-', 10) 
		+ "\n" + repeatChar('*', 65));

	resetValues();
}

//-- Find the word according to the final count.
function FindLastAlphabet(MatchlessCount)
{	
	var existRemainNumber, remainNumber;
	
	remainNumber = MatchlessCount % 6;
	if(remainNumber == 0) remainNumber = 6; 
	
	extractChar = strGame.charAt(remainNumber-1);
	strGame = doRemoveChar(strGame, extractChar);
	positionIndex = remainNumber;

	remainNumber = MatchlessCount % 5;
	if(remainNumber == 0) remainNumber = 5; 	
	positionIndex = getRemainRotationNumber(remainNumber);
	
	remainNumber = MatchlessCount % 4;
	if(remainNumber == 0) remainNumber = 4;
	positionIndex = getRemainRotationNumber(remainNumber);
	
	remainNumber = MatchlessCount % 3;
	if(remainNumber == 0) remainNumber = 3;
	positionIndex = getRemainRotationNumber(remainNumber);
	
	remainNumber = MatchlessCount % 2;
	if(remainNumber == 0) remainNumber = 2;
	positionIndex = getRemainRotationNumber(remainNumber);
	
	return getWord(strGame);
}

//-------- Rotate the FLAMES word if the count exceeds total count. ---
function getRemainRotationNumber(remainNumber)
{

	var removableCharIndex = positionIndex + (remainNumber - 1);
	if(removableCharIndex <= strGame.length)
	{
		extractChar = strGame.charAt(removableCharIndex - 1);		
		strGame = doRemoveChar(strGame, extractChar);
	}
	else
	{		
		removableCharIndex = removableCharIndex - strGame.length;
		extractChar = strGame.charAt(removableCharIndex - 1);
		strGame = doRemoveChar(strGame, extractChar);
	}
	
	return removableCharIndex;
}
//----------------------------------------------------------------------
</script>

// -- Validation Related -- 
//-- { str => Steve;  removeChar => e } -- Result -- { Stve } 
function doRemoveChar(str, removeChar)
{
	var findIndex = doExist(str, removeChar);
	var result = "";
	if(findIndex == 0)
		result = str.substring(1);
	if(findIndex > 0)
	{
		result = str.substring(0, findIndex);
		result += str.substring(findIndex + 1);  
	}
	
  return result;
}

 // -- Check the Character exists in a String -- 
function Validate(fieldValue)
{
	var objRegExp=/^[a-zA-Z]$/;
	return objRegExp.test(fieldValue);
}

 // -- Check the Character exists in a String -- 
function doExist(str, chr)
{
	for(var i=0; i<str.length; i++)
	{
		if(str.charAt(i) == chr)
		{		
			return i;
		}
	}
	return -1;
}

// -- Trim the space from the string anywhere at the string -- 
function trimSpace(str)
{  
  var arrTrimmedString = new Array();
  var strLength = str.length;
  for(var i=0; i<strLength; i++)
  {
	if(str.charAt(i) == ' ')
		continue;
	else
		arrTrimmedString[arrTrimmedString.length] = str.charAt(i);
  }

  return arrTrimmedString.join("");
}

// -- Repeat the Character at 'n' times.
function repeatChar(repeatChr, repeatTimes)
{
	var finalString = "";
	for(var i=0; i<repeatTimes; i++)
	{
		finalString += repeatChr;
	}
	return finalString;
}

 // -- Reset the global variable for the new input values. 
function resetValues()
{
	strGame = "FLAMES";
	positionIndex = -1;
	removableCharIndex = -1;
}

 //-- Validate the input values -- 
//-- Validate the input values --
function validateInputValues(strName1, strName2, frmObject)
{
if(strName1.length == 0)
{
	alert("\n"+ repeatChar('*', 65) 
	    +" \n Validation:\n \t- Please provide Your Name. \n" 
	    + repeatChar('*', 65));
	frmObject.Name1.select();
	frmObject.Name1.focus();
	return false;
}

if(Validate(strName1) == false)
{
	alert("\n"+ repeatChar('*', 65) 
		+" \n Validation:\n " 
		+ "\t- Hey... How can Your name other than alphabets. :-) \n" 
		+ repeatChar('*', 65));
	frmObject.Name1.select();
	frmObject.Name1.focus();
	return false;
}

if(strName2.length == 0)
{
	alert("\n"+ repeatChar('*', 65) 
		+" \n Validation:\n \t- Please provide Girl Name. \n" 
		+ repeatChar('*', 65));
	frmObject.Name2.select();
	frmObject.Name2.focus();
	return false;
}

if(Validate(strName2) == false)
{
	alert("\n"+ repeatChar('*', 65) 
		+" \n Validation:\n " 
		+ "\t- Hey... Girl name can't be other than alphabets. :-) \n" 
		+ repeatChar('*', 65));
	frmObject.Name2.select();
	frmObject.Name2.focus();
	return false;
}

	if(strName1 == strName2)
	{
		alert("\n"+ repeatChar('*', 65) 
			+" \n Validation:\n \t- Both of their names are same. " 
			+ " hey, don't cheat me Ok va. \n" 
			+ repeatChar('*', 65));
		frmObject.Name2.select();
		frmObject.Name2.focus();
		return false;
	}
}

 //-- Pull the word by passing the Character. 
function getWord(strSymbol)
{
	switch(strSymbol)
	{
		case "F":
		    return "Friend";
			break;
		case "L":
		    return "Love";
			break;
		case "A":
		    return "Affection";
			break;
		case "M":
		    return "Most Beloved";
			break;
		case "E":
		    return "Enemy";
			break;
		case "S":
		    return "Sister";
			break;
	}
}

Conclusion

Hope you had fun by playing a game and looking at the code as well. Cheers…

top
Add a Comment
Name:
Email:
Comments:

Stevebin