user6148842
user6148842

Reputation:

Understand Javascript error

This is my very first program in javascript! I've been trying to go through tutorials and I kind of get it. But my code doesn't work! I want to find out why??? An explanation of the error would help me understand.

document.write("Random # (1-5) = ", Math.floor((Math.random() * 5) + 1), "<br />");
if(Random = 2){
 alert("Your names Bob");
}else {
 alert("Your names Superman");
}

This program writes to the page a random number between 1 and 5, (works fine...) then, say, if the random number is 2, I want to alert the user that their name is Bob, and if the number isn't 2, alert them that their name is Superman...

Complete noob/beginner here, sorry if this frustrates you, but I appreciate your understanding! The only way to learn!!! Cheers :)

Upvotes: 0

Views: 58

Answers (1)

wezzy
wezzy

Reputation: 5935

your code is wrong, the first thing is that you should use "==" for comparison into if() (or "===" if you also want to check the type).

The second thing that I see is that you are checking the value of a variable called Random but the first line wrote some text on the page (document.write()) but it doesn't set the value of the Random variable, try replacing the first line with:

var Random = Math.floor((Math.random() * 5) + 1)

and this should work

Upvotes: 3

Related Questions