Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NaN Property

Status
Not open for further replies.

robbied69

IS-IT--Management
Sep 22, 2006
34
GB
Hi,

I am a newbie to JavaScript. I have created the following script just to generate a random number and perform a few tasks on it:

<html>
<head>
<title>Random Generator</title>
<script LANGUAGE = "JavaScript">

//create a variable 'rand1' to get a random number between 0.0 to 1.0
var rand1 = Math.random();

//create a variable 'rand2' which gets the random value rand1 and and makes it between 0.0 and 10.0
var rand2 = rand1 * 10;

//create a variable 'rand3' which gets the value of rand2 and rounds it to make in between 1 and 10.
var rand3 = Math.ceil(rand3);

//add each part to a string
var str = "Random float [rand1]: " + rand1 + "\n";
str += "Sepcify range [rand2]: " + rand2 + "\n";
str += "Random Integer [rand3]: " + rand3 + "\n";

//display the string
alert(str);

</script>
</head>

<body>
Random number generator
</body>
</html>

When I recieve the output, the last line reads:

Random integer [rand3]: NaN

After checking google I found that NaN stands for Not a number. Can anyone see why I get this?

Thanks in advance

Rob
 
>[tt]var rand3 = Math.ceil(rand[highlight]3[/highlight]);[/tt] //?????
 
//create a variable 'rand3' which gets the [!]value of rand2[/!] and rounds it to make in between 1 and 10.
var rand3 = Math.ceil(rand[!]2[/!]);


[monkey][snake] <.
 
Thanks both of you. I managed to find it also. Many thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top