JavaScript - Function with returning value

Microsoft Windows 7 professional 32-bit
June 9, 2010 at 09:39:59
Specs: Windows 7 Professional, Intel Core 2 Duo, 1GB
Hi,

Can anyone please help me with a function which returns a value in JavaScript?

The function contains an IF statement to check a number input by the user. If the number is illegal, the user is prompted of this. The function is then re-called to re-prompt for a legal value.

After inputting an illegal value and then re-entering a legal value, it is the old illegal value that is retained and returned to the main program.

Can anyone tell me why the code is retaining this old value. I cannot see how it is possible.

Thanks


See More: JavaScript - Function with returning value

Report •


#1
June 9, 2010 at 09:42:39
Here's the code:

function getChosenNumber(playersNumber)
{
playersNumber = window.prompt('Player ' + currentPlayer + ': Please enter either 1, 2 OR 3.'); //Prompts the current player for a legal number and converts to a number

if ((playersNumber != 1) && (playersNumber != 2) && (playersNumber != 3)) //Analyses whether the number is not equal to 1 and 2 and 3
{
window.alert("Illegal selection! Please enter either 1, 2 OR 3."); //Alterts the user if the above statement is true i.e illegal number entered
getChosenNumber(playersNumber) //Re-calls the getChosenNumber function
}
playersNumber = parseFloat(playersNumber);
return playersNumber;
}


Report •

#2
June 9, 2010 at 11:17:58
Replace
getChosenNumber(playersNumber) //Re-calls the getChosenNumber function

With:
playersNumber = getChosenNumber(playersNumber);


Report •

#3
June 10, 2010 at 09:21:38
Worked perfectly - thank you!

Report •

Related Solutions


Ask Question