- 1

Random int generator + if / else if / else ) 1 through 5 . .. always returning false .. why??

<button onclick="func()">push</button> <p id="in"></p> <p id="out"></p> <script> var x = document.getElementById("in") x.innerHTML = Math.floor((Math.random() *5) + 1 ); if ( x < 3 ) { document.getElementById("out").innerHTML = "too low"; } else.if ( x == ) { document.getElementById("out").innerHTML = "bingo"; } else { document.getElementById("out").innerHTML = " too high" } </script> </body> </html> no matter the number. the contents of id="out" is always displaying "too high" ... aka FALSE

23rd Feb 2017, 5:04 PM
Sonny mbiam
Sonny mbiam - avatar
4 Réponses
+ 16
First of all, you have a syntax error at the else if statement. Then, it says func is not defined. You need to include everything under that function
23rd Feb 2017, 5:41 PM
Gami
Gami - avatar
+ 16
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button onclick="func()">push</button> <p id="in"></p> <p id="out"></p> <script> function func() { var x = document.getElementById("in") x.innerHTML = Math.floor((Math.random() *5) + 1 ); if ( x < 3 ) { document.getElementById("out").innerHTML = "too low"; } else if ( x == 2) { document.getElementById("out").innerHTML = "bingo"; } else { document.getElementById("out").innerHTML = " too high" } } </script> </body> </html> I fixed it!
23rd Feb 2017, 5:43 PM
Gami
Gami - avatar
+ 2
else.if should be else if
23rd Feb 2017, 5:41 PM
John Jeffries
John Jeffries - avatar
+ 1
sorry that was a typo. but in my actual document it does read else if
23rd Feb 2017, 5:43 PM
Sonny mbiam
Sonny mbiam - avatar