I am trying to make Rock, Paper, Scissors via The Odin Project, and I’m stuck in the last step. I don’t seem to have trouble getting the prompt to work, just can’t seem to either tally up points, or I’m having trouble ending the game when either the player or computer gets to 5 points. I did include some console.logs at the bottom, but I’m unable to actually see the score due to the endless prompts. What am I doing wrong? I just need the game to end when either the player or computer gets to 5 points.
let playerScore = 0;
let computerScore = 0;
function computerPlay() {
let allChoices = ["Rock", "Paper", "Scissors"];
let randomChoice = allChoices[Math.floor(Math.random() * allChoices.length)];
return randomChoice
}
function game() {
while (playerScore <= 5 || computerScore <= 5) {
const playerSelection = prompt("Would you like to choose R, P or S?")
const computerSelection = computerPlay();
alert(playRound(playerSelection, computerSelection));
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == computerSelection) {
return "Tie game!"
} else if (playerSelection == "Rock" && computerSelection == "Scissors") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else if (playerSelection == "Paper" && computerSelection == "Rock") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else if (playerSelection == "Scissors" && computerSelection == "Paper") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else {
return `You lose! ${computerSelection} beats ${playerSelection}`
computerScore += 1;
}
}
function winGame() {
if (playerScore == 5) {
return "You win!"
} else if (computerScore == 5) {
return "You lose!"
}
}
game();
console.log(playerScore);
console.log(computerScore);
>Solution :
function game() {
while (playerScore < 5 && computerScore < 5) {
const playerSelection = prompt("Would you like to choose Rock, Paper or Scissors?")
const computerSelection = computerPlay();
alert(playRound(playerSelection, computerSelection));
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "Tie game!"
} else if (playerSelection === "Rock" && computerSelection === "Scissors") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else if (playerSelection === "Paper" && computerSelection === "Rock") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else if (playerSelection === "Scissors" && computerSelection === "Paper") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else {
computerScore += 1;
return `You lose! ${computerSelection} beats ${playerSelection}`
}
}
You returned from the function before incrementing the points. So they will never be incremented. Also (playerScore <= 5 || computerScore <= 5) means, that the game will go on until both – playerScore and computerScore are below 6. It has to stop when one of them reaches 5