javascript - How do I make images change with a rock, paper, scissors game? -
i'm trying build rock paper scissors game in javascript, have computer outputs images html document. i've tried know, , won't work. here's html:
<!doctype html> <html> <head> <script src="rock paper scissors.js"></script> </head> <body> <p>your choice:<img id="myimage" onafterprint="userimgdisplay()" src="blank.png" width="250px" height="250px"/>the computer's choice:<img id="myimage" onload="comimgdisplay()" src="blank.png" width="250px" height="250px"/> </p>`
and here's javascript:
var userchoice = prompt("do choose rock, paper or scissors?","don't use articles (i.e. the, a, etc.)").tolowercase(); var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock"; } else if(computerchoice <= 0.67) { computerchoice = "paper"; } else { computerchoice = "scissors"; } function userimgdisplay() { var image = document.getelementbyid('myimage'); if (userchoice = "rock") { image.src = "rock.png"; }else if (userchoice = "paper") { image.src = "paper.png"; }else if (userchoice = "scissors") { image.src = "scissors.png"; } } function comimgdisplay() { var image = document.getelementbyid('myimage'); if (computerchoice = "rock") { image.src = "rock.png"; }else if (computerchoice = "paper") { image.src = "paper.png"; }else if (computerchoice = "scissors") { image.src = "scissors.png"; } }
the "rock.png", "paper.png", , "scissors.png" based on respective items, , "blank.png" transparent image. program display "rock.png" choice , nothing computer's. doing wrong?
your logic not right, using =
should ==
for example @ changes method, have issues in methods.
function comimgdisplay() { var image = document.getelementbyid('myimage'); if (computerchoice == "rock") { image.src = "rock.png"; }else if (computerchoice == "paper") { image.src = "paper.png"; }else if (computerchoice =="scissors") { image.src = "scissors.png"; } }
for simplicity this:
function comimgdisplay() { var image = document.getelementbyid('myimage'); image.src = computerchoice + ".png"; }
Comments
Post a Comment