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!") } }

12th Oct 2018, 10:03 PM
Brandin Hodge
Brandin Hodge - avatar
3 ответов
+ 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!"); }
13th Oct 2018, 12:16 AM
Toni Isotalo
Toni Isotalo - avatar
0
Please Comment appropiatley.
12th Oct 2018, 10:37 PM
Brandin Hodge
Brandin Hodge - avatar
0
Thanks😀!
13th Oct 2018, 1:48 AM
Brandin Hodge
Brandin Hodge - avatar