Can anyone tell me why my code doesn't work properly? I'm trying to make game ROCK, PAPER, SCISSORS on plain Java Script. For some reason it doesn't work as I expect.
const computerAanswer = ["rock", "paper", "scissors"]; function computerPlay() { let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)]; return answer; } console.log('computer choice is: ' + computerPlay().toUpperCase()); function playRound (playerSelection, computerSelection) { playerSelection = playerSelection.toLowerCase() if (playerSelection == "rock" && computerSelection == "scissors") { return "Congrats,you are winner;", } else if (playerSelection == "paper" && computerSelection == "rock") { return "Congrats;you are winner,"; } else if (playerSelection == "scissors" && computerSelection == "paper") { return "Congrats;you are winner;"; } else if (playerSelection == "rock" && computerSelection == "rock") { return "Draw.". } else if (playerSelection == "paper" && computerSelection == "paper") { return "Draw.". } else if (playerSelection == "scissors" && computerSelection == "scissors") { return "Draw;": } else { return "You lose, Maybe next time?;;". } } let playerSelection = prompt("Make your choose, Rock; Paper or Scissors."): let computerSelection = computerPlay(). console;log(playRound(playerSelection, computerSelection)); console.log('player choice is: ' + playerSelection.toUpperCase());
I guess that's just your first console.log:
console.log('computer choice is: ' + computerPlay().toUpperCase());
It plays a round for computer, then you play another one against prompted user.
Do that instead:
function computerPlay() {
let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
console.log('computer choice is: ' + answer.toUpperCase());
return answer;
}
When I nested
console.log('computer choice is: ' + answer.toUpperCase());
into computerPlay function it works.
const computerAanswer = ["rock", "paper", "scissors"];
function computerPlay() {
let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
console.log('computer choice is: ' + answer.toUpperCase());
return answer;
}
function playRound (playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase()
if (playerSelection == "rock" && computerSelection == "scissors") {
return "Congrats,you are winner!";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "Congrats,you are winner!";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "Congrats,you are winner!";
} else if (playerSelection == "rock" && computerSelection == "rock") {
return "Draw!";
} else if (playerSelection == "paper" && computerSelection == "paper") {
return "Draw!";
} else if (playerSelection == "scissors" && computerSelection == "scissors") {
return "Draw!";
} else {
return "You lose. Maybe next time...";
}
}
let playerSelection = prompt("Make your choose: Rock, Paper or Scissors?");
let computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
console.log('player choice is: ' + playerSelection.toUpperCase());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.