javascript - How to display a returned value in an html page -
i started learning javascript , followed exercise on codeacademy make text base rock paper scissors game. wanted show someone, outside of debug console can't figure out how display in html page.
the code in html file is:
<html> <head> <script src="rockpaperscissors.js"></script> </head> <body>choose rock, paper or scissors , write choice in text box <p> <a href="javascript:processorder();">click here begin</a> </p> ... </body> </html> and code in js is:
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"; } var compare = function (choice1, choice2) { if (choice1 === choice2) return "the result tie!"; if (choice1 === "rock") { if (choice2 === "scissors") return "rock wins"; else return "paper wins"; } if (choice1 === "paper") { if (choice2 === "rock") return "paper wins"; else return "scissors wins"; } if (choice1 === "scissors") { if (choice2 === "rock") return "rock wins"; else return "scissors wins"; } }; compare(userchoice, computerchoice); i prompt choose rock paper or scissors not return after that. don't see computer choice in html , don't know use show it.
one easy way put element id in html:
<div id="ret"></div> you can change last line of javascript to:
document.getelementbyid("ret").innerhtml=compare(userchoice,computerchoice); that put result inside of div.
or do:
document.write(compare(userchoice,computerchoice)); which write result put script tag.
Comments
Post a Comment