Rockpaperscissors Javascript Project

Although i’m getting no syntax errors, my code will not execute properly. When I have the value of ‘rock’ attached to userChoice variable, and the computerChoice variable randomly chooses ‘scissors’, it still prints “Its a tie!”. // output: You threw rock and the computer threw scissors. Its a tie!

I’ve looked over others code for this same project, and I was unable to find any discrepancies that would alter the way the code was logged to the console.

It would be great if someone could look over what I have so far and give me some pointers on how to get it to log the proper string to the console from the conditional statement, or in general on how to continue to improve my code for the future.

const getUserChoice = (userInput) => {

userInput = userInput.toLowerCase();

if(userInput === “rock” || userInput === “paper” || userInput === “scissors”) {

return userInput;

} else {

console.log("ERROR: " + userInput)

}

};

const getComputerChoice = () => {

let randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {

case 0:

  return "rock";

case 1:

  return "scissors";

case 2:

  return "paper";

}

};

const determineWinner = (userChoice, computerChoice) => {

userChoice = getUserChoice(userChoice);

computerChoice = getComputerChoice(computerChoice);

if(userChoice = computerChoice) {

return "Its a tie!";

}

if(userChoice === “rock”) {

if(computerChoice === "paper") {

  return "Computer Won!";

} else {

  return "You Won!";

}

}

if(userChoice === “paper”) {

if(computerChoice === "scissors") {

  return "Computer Won!";

} else {

  return "You Won!";

}

}

if(userChoice === “scissors”) {

if(computerChoice === "rock") {

  return "Computer Won!";

} else {

  return "You Won!";

}    

}

};

const playGame = () => {

const userChoice = getUserChoice(‘rock’);

const computerChoice = getComputerChoice();

console.log(You threw ${userChoice} and the computer threw ${computerChoice}.);

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

}

playGame();

Hi and welcome to the forums!

One issue that is causing problems is this line:

    if(userChoice = computerChoice) {

Make sure you use 3 or 2 equal signs when you compare two variables/constants. One equal sign is used for assigning a new value to a variable which is not what you want to do in this comparison.

Second problem is inside the determineWinner function where you have these 2 lines of code:

    userChoice = getUserChoice(userChoice);
    
    computerChoice = getComputerChoice(computerChoice);

Comment out or remove those 2 lines of code in the determine winner function. You have already made a computer and user-choice inside of the playGame function so you don’t need to call those functions again.

Some suggestions would be to do console.log on variables if you run into unexpected behavior like this. Also read error messages carefully when you get them and they will make more sense after a while but in this case there was no error message… Your code will generally improve over time as you gain more experience in JS and become both shorter and more readable as long as you keep practicing. Also, a small heads up for next time, when posting code in this forum there is a little </> symbol in the post-editor which will format the code correctly…

1 Like

Thank you for the feedback! It works! I’m still getting used to the forum, and I have just read over the guide on how it works. I appreciate the in-depth response and the suggestions!

1 Like