JavaScript not executing in my HTML -
so i've begun learning html/css/javascript , came across issue while trying make exceedingly simple version of rock-paper-scissors. believe code works fine, can't check because it refuses execute. have looked extensively answers on internet, can't seem find any. i have little experience in javascript, learning off of codecademy think resource may outdated other websites appear have conflicting syntax. in short, what doing wrong, , website has right?
<html> <head> <title>r,p,s!</title> <script type="text/javascript"> function whole(){ function game(play){ if (play="yes"){ var userchoice = prompt("do choose rock, paper or scissors?"); var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock";} else if(computerchoice <= 0.67) { computerchoice = "paper";} else { computerchoice = "scissors"; } function compare(choice1,choice2){ if (choice1==choice2){ compare(userchoice,computerchoice); } if (choice1=="rock"){ if (choice2=="scissors"){ document.getelementbyid("result").innerhtml="rock wins"; } else{ document.getelementbyid("result").innerhtml="paper wins"; } } if (choice1=="paper"){ if (choice2=="rock"){ document.getelementbyid("result").innerhtml="paper wins"; } else{ document.getelementbyid("result").innerhtml="scissors win"; } } if (choice1=="scissors"){ if (choice2=="paper"){ document.getelementbyid("result").innerhtml="scissors win"; } else{ document.getelementbyid("result").innerhtml="rock wins"; } } }; compare(userchoice,computerchoice); } else{ document.writeln("<p>thanks playing! made alex</p>";) } } var start = prompt ("do want play?","yes");} </script> </head> <body style="text-align:center"> <h1>javascript on webpage? madness!</h1> <h2>madness? this... is... html!!!!</h2> <button onclick="whole()">try out!</button> <p id="result">who won?</p> </body> </html>
**edit: appears codecademy's glossary agrees other websites, haven't gotten around editing lessons yet.*
**edit: here's final little code it. enjoy simplicity!*
<html> <head> <title>r,p,s!</title> <script type="text/javascript"> function whole(){ function game(play){ if (play=="yes"||play=="yes"){ var userchoice = prompt("do choose rock, paper or scissors?"); var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock";} else if(computerchoice <= 0.67) { computerchoice = "paper";} else { computerchoice = "scissors"; } function compare(choice1,choice2){ if (choice1==choice2){ alert("it tie!"); game("yes"); } if (choice1=="rock"){ if (choice2=="scissors"){ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("win").innerhtml="you win. rock crushes scissors."; document.getelementbyid("loss").innerhtml=""; } else{ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("loss").innerhtml="you lose. paper smothers rock."; document.getelementbyid("win").innerhtml=""; } } else if (choice1=="paper"){ if (choice2=="rock"){ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("win").innerhtml="you win. paper smothers rock."; document.getelementbyid("loss").innerhtml=""; } else{ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("loss").innerhtml="you lose. scissors cut paper."; document.getelementbyid("win").innerhtml=""; } } else if (choice1=="scissors"){ if (choice2=="paper"){ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("win").innerhtml="you win. scissors cut paper."; document.getelementbyid("loss").innerhtml=""; } else{ document.getelementbyid("messages").innerhtml=""; document.getelementbyid("loss").innerhtml="you lose. rock crushes scissors."; document.getelementbyid("win").innerhtml=""; } } else{ alert("very funny. right."); game("yes"); } }; compare(userchoice,computerchoice); } else{ document.getelementbyid("messages").innerhtml="well alrighty then."; document.getelementbyid("loss").innerhtml=""; document.getelementbyid("win").innerhtml=""; } } var start = prompt ("do want play?","yes"); game(start);} </script> <style> body{ text-align:center; } #messages{ font-size:20px; color: #00246b; } #win{ color: #29a329; font-size:18px; } #loss{ color:#cc0000; font-size:18px; } a{ text-decoration:none; color:black; } a:hover{ font-size:125%; color:#b20000; } button{ font-size:21px; } </style> </head> <body> <a href="http://youtu.be/t8r3cwm4jii"> <h1>javascript on webpage? madness!</h1> <h2>madness? this... is... html!!!!</h2> </a> <button onclick="whole()">try out!</button> <p id="messages">who won?</p> <p class="result"><span id="loss"></span><span id="win"></span></p> </body> </html>
your code syntatically wrong. put semicolon after paranthesis.
else { document.writeln("<p>thanks playing! made alex</p>"); }
now call function game inside closure @ end of function whole.
ie
function() whole(){ function game(play) { // logic } var start = prompt("do want play?", "yes"); game(start); }
your game logic error-prone. recheck logic.
you calling function compare recursively infinitely when choice1 == choice2
just comment line
if(choice1 == choice2) { // compare(userchoice, computerchoice); }
it work
Comments
Post a Comment