0
I need help!
I made this code but it when I press heads it says you lose, and you win when I press tails. Can you help? HTML ==================================== <!DOCTYPE html> <html> <head> <title>Heads or Tails</title> </head> <body> <h1>Coin Flip</h1> <hr /> <button onclick="heads()">Heads</button> <button onclick="tails()">Tails</button> </body> </html> Java Script ==================================== var flip = 0 function heads() { flip = Math.random() if(flip=0) { alert("You Win!") } else { alert("You Lose, Try Again!") } } function tails() { flip = Math.random() if(flip=1) { alert("You Win!") } else { alert("You Lose, Try Again!") } }
3 Réponses
+ 5
Math.random() returns a floating point value.
Which means your statements will always be false.
You need to round the value to the nearest integer.
Or you could check if the value is greater or less than 0.5.
Also use double equals ==
var flip = Math.round(Math.random())
if(flip == 1) {
alert("You Win!");
}
0
Please Comment appropiatley.
0
Thanks😀!