Rock, paper and scissors project for JS

Hi guys, been having a problem with the rps taht i just can’t figure out.
Every time I run the game I always get the answer as “Congratulations, you won!”, regardless of what’s thrown.

Here’s my code below.

console.log(“hi”);
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
console.log(“Error, please type rock, paper or scissors”);
}
};

const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return “rock;”;
case 1:
return “paper;”;
case 2:
return “scissors;”;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game is a tie!”;
}
if (userChoice === “rock”) {
if (computerChoice === “paper”)
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}

if (userChoice === “scissors”) {
if (computerChoice === “rock”)
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”)
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}

};
const playgame = () => {
let userChoice = getUserChoice(“scissors”);
let computerChoice = getComputerChoice();
console.log(You threw: ${userChoice});
console.log(The computer threw: ${computerChoice});

console.log(determineWinner(userChoice, computerChoice));
};

playgame();

Any and all help greatly appreciated.

Dave

In getComputerChoice,

return "rock;";
// Same mistake with "paper;" and "scissors;"

In determineWinner,

// Indentations just for illustrative purposes to make clear which if block is
// getting paired with the else block. 

// You wrote:
if (userChoice === "rock") {
if (computerChoice === "paper")
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
// which is equivalent to:
if (userChoice === "rock") {
    if (computerChoice === "paper") {
        return "Sorry, computer won!";
    }
} else {
    return "Congratulations, you won!";
}

// It should be:
if (userChoice === "rock") {
    if (computerChoice === "paper") {
        return "Sorry, computer won!";
    } else {
        return "Congratulations, you won!";
    }
}

// Same issue with the curly braces in the other two blocks

Thank you so much for your help!

1 Like