I'm having problems getting my simple Rock Paper Scissors game to display the winner of each game. Please help.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if(userInput ==="paper" || userInput ==="rock" || userInput ==="scissors"){
console.log("You chose " `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
const computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" ` ${computerInput}`);
if (computerInput <= 0.33) {
computerInput = "rock";
}
else if (computerInput > 0.66) {
computerInput = "paper";
}
else {
computerInput = "scissors";
}
// DECLARE WINNER
const winner = declareWinner (userInput, computerInput);
if (userInput === 'rock' && computerInput === 'paper') {
console.log ('You lose! Rock beats Paper');
}
else if (userInput === 'scissors' && computerInput === 'paper') {
console.log ('You win! Scissors beats Paper');
}
else {
console.log ('You tie!');
}
I get an error message "Uncaught TypeError: Assignment to constant variable. at index.js:22:23" in the line of code below ```computerInput = "scissors"; `
else {
computerInput = "scissors";
};
What am I doing wrong?
CodePudding user response:
There were two problems with the code:-
- You are doing
choices[Math.floor(Math.random() * 3)];which results in a random value fromchoicesarray, so there is no need for multiple if-else statements that were after it. - You were missing the
declareWinnerfunction declaration.
Also, You are missing some conditions in declareWinner function, like userInput === 'paper' && computerInput === 'rock', userInput === 'rock' && computerInput === 'scissors' etc.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random()*3)];
console.log("Computer chose" ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
if (userInput === 'rock' && computerInput === 'paper') {
console.log('You lose! Rock beats Paper');
} else if (userInput === 'scissors' && computerInput === 'paper') {
console.log('You win! Scissors beats Paper');
} else {
console.log('You tie!');
}
}
Tip:- You can use an object to choose the winner in declareWinner function.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
let win = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
if (win[userInput] === computerInput) {
console.log(`You won! ${userInput} beats ${computerInput}`);
} else if (win[computerInput] === userInput) {
console.log(`You lost! ${computerInput} beats ${userInput}`);
} else console.log("Tie")
}
CodePudding user response:
You can't change a const value. Use let instead:
let computerInput = choices[Math.floor(Math.random() * 3)];
