Rock, Paper, Scissors in JavaScript -
i'm working on making first game (rock paper sissors) , ran issue when userchoice scissors , computerchoice rock, program cannot return winner rock. can program give me winner other combination.
i have code here:
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 { if(choice2 === "scissors") { return "scissors wins"; } } if(choice1 === "scissors") { if(choice2 === "rock") { return "rock wins"; } else { if(choice2 === "paper") { return "scissors wins"; } } } } }; console.log("user choice: " + userchoice); console.log("computer choice: " + computerchoice); compare(userchoice, computerchoice);
you unable see issue due poor indentation of code. indented issue clear:
if (choice1 === "paper") { if (choice2 === "rock") { return "paper wins"; } else { if (choice2 === "scissors") { return "scissors wins"; } } if (choice1 === "scissors") { if (choice2 === "rock") { return "rock wins"; } else { if (choice2 === "paper") { return "scissors wins"; } } } }
your if (choice1 === "scissors") {
within if (choice1 === "paper") {
. code within never reached.
Comments
Post a Comment