Rock-paper-scissors, computer chooses only 'paper'

Hey! I am doing rock-paper-scissors exercise and I can’t understand, why computer gives everytime ‘paper’? Did I write something wrong? It does launches, it does change the output text depending if human wins or loses, but computer is just not changing it’s choice, it’s always ‘paper’. Can someone sort this question out for me?

Here is my code:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput===‘paper’ || userInput===‘scissors’){
return userInput;
} else {
console.log(‘There is an error.’);
}
};

const getComputerChoice = () => {
let choice = Math.floor(Math.random()*3);
if (choice = 0) {
return ‘rock’;
} else if (choice = 1) {
return ‘paper’;
} else if (choice = 2) {
return ‘scissors’;
};
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return ‘There was a tie’;
}

if (userChoice === ‘paper’){
if (computerChoice === ‘rock’) {
return ‘You won!’;
} else {
return ‘Computer won!’;
}
}

if (userChoice === ‘rock’){
if (computerChoice === ‘scissors’) {
return ‘You won!’;
} else {
return ‘Computer won!’;
}
}

if (userChoice === ‘scissors’){
if (computerChoice === ‘paper’) {
return ‘You won!’;
} else {
return ‘Computer won!’;
}
}
};

const playGame = () => {
let userChoice = getUserChoice(‘rock’);
console.log('You choose '+ userChoice);
let computerChoice = getComputerChoice();
console.log('Computer choose ’ + computerChoice);
console.log (determineWinner(userChoice, computerChoice));
};

playGame();

For some reason, when I changed if operator to switch keyword in part of my code, where computer choosed what to throw depending on a random number, everything started working perfectly. Now I have question - why?

I’m not completely sure why this is only returning paper, but here:

const getComputerChoice = () => {
let choice = Math.floor(Math.random()*3);
if (choice = 0) {
return ‘rock’;
} else if (choice = 1) {
return ‘paper’;
} else if (choice = 2) {
return ‘scissors’;
};
};

You did choice = number, instead of doing choice === number.
Usually, one equal sign means you are reassigning choice to that number.

1 Like

Thanks for so fast answer and for solution! I still can’t remember naturally, that there is a difference between = and ===

1 Like

No problem! It messes me up too. I’m surprised my answer made sense, because I’m still a beginner to JavaScript too lol.

2 Likes