Rock Paper Scissors Project. https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

Hello im currently working on the “rock paper scissors” project and cant figure out why its only printing “rock, rock” “paper,rock” ect. heres my code.

//Determines and prints users choice

const getUserChoice = userInput => {

  userInput = userInput.toLowerCase();

  if (userInput === "rock" || userInput === "paper" || userInput === "scissors") {

    return userInput

  } else {

    console.log("Invalid Choice")

  }

};

//Generates computers Choice

function getComputerChoice() {

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

  switch (randomNumber) {

    case 0:

      return "rock";

    case 1:

      return "paper";

    case 2:

      return "scissors"

    break;

  }

}

//Determines Winner

function determineWinner (userChoice,computerChoice) {

  if (userChoice === computerChoice) {

    console.log("Tie!")

  };

  if (userChoice === "rock") {

    if (computerChoice === "paper") {

      console.log("You Won!!!")

    }

  };

  if (userChoice === "paper") {

    if (computerChoice === "rock") {

      console.log("You Win!!!")

    } else if (computerChoice === "scissors") {

      console.log("You Loose! :(")

    }

  };

  if (userChoice === "scissors") {

    if (computerChoice === "paper") {

      console.log("You Win!")

    } else if (computerChoice === "rock") {

      console.log("You Loose! :(")

    }

  };

};

function playGame() {

  let userChoice = getUserChoice ("rock", "paper", "scissors")

  let computerChoice = getComputerChoice()

  console.log(userChoice,computerChoice)

}

playGame()

What else do you expect to see in the output then?

1 Like

If you mean because userChoice always equals rock, look carefully at your getUserChoice function.

const getUserChoice = userInput => {
// rest of the code here
}

getUserChoice() only takes one input. Now look at your playGame() function:

function playGame() {

  let userChoice = getUserChoice ("rock", "paper", "scissors")
  // rest of the code here
}

You’re trying to pass three inputs: ("rock", "paper", "scissors"). Only the first is accepted, therefore userInput is always rock. You’re supposed to manually change the userInput (you could use random generation with let userChoice = getComputerChoice(), but later you’ll be programming in a cheat code, and that shouldn’t be an answer the computer can generate).

If you’re talking about the lack of a console log for the winner, function playGame() only has one line logging anything to the console, you haven’t called determineWinner() within playGame() yet.

NOTE: You’re leaving out a LOT semi-colons. In JavaScript, it’s considered good practice to end each line with ; to prevent unexpected behavior, even though the code will usually run correctly without them.

1 Like

i just realized my mistakes. Mistake #1 coding while very tired… didnt have a clear mindset lol. My second mistake was i didnt call the function determineWinner() at the end but i added that now so it prints to the console the winner. However now it prints the winner, but it only comes up as “Tie!” no matter who wins or looses.

function determineWinner (userChoice,computerChoice) {
  if (userChoice === computerChoice) {
  // code
  };

  if (userChoice === "rock") {
    // code
  };

  if (userChoice === "paper") {
    // code
  };

  if (userChoice === "scissors") {
    // code
  };
};

I’d make these else if (except the first). If userChoice === computerChoice, you don’t need to evaluate the other ifs, else if userChoice === "rock", you don’t need to evaluate the rest of the ifs, etc.

What is your current code?