Rock, Paper, Scissors, Project: Computer Only Returns Paper

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors-javascript

Hello! I’m sure there’s some slight oversight on my part but I can’t figure out the problem. Every time I run my code the computer choice is always 'paper'. The user choice input works fine though.

My code is:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase()
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput
  } else {
    console.log('Error! Invalid input.')
  }
};

function getComputerChoice() {
  Math.floor(Math.random() * 3)
    if (getComputerChoice = 0) {
    return 'rock'
  } else if (getComputerChoice = 1) {
    return 'paper'
  } else if (getComputerChoice = 2) {
    return 'scissors'
  } 
}

console.log(getComputerChoice());

(I also realize that I’m missing semicolons, but it’s hard for me to remember where they go, so any extra tips on that would be appreciated too!)

Consider what this actually means:

getComputerChoice has not been previously declared or assigned. Even if it had, you are using =, the assignment operator, so you are assigning 0 to a new variable with global scope named getComputerChoice. 0 has a boolean value of false, so the first if block is skipped. Can you guess what happens next?

Okay, so it automatically just picks 1 every time, but wouldn’t it at least switch between 1 & 2 if it’s supposed to be a random number? I had === for each if/else statement originally, but that would only return undefined. The project also said to create the function with no parameters, so that why I didn’t assign it anything. Does no parameters actually mean something else?

You have the code to generate a random number, but you don’t assign a variable to the number, so it just vanishes after being generated:

If you assign a variable to the random number, then you can compare that variable to the values in your if conditions. The reason you currently get paper returned every time is because you assign the value 1 which is truthy to the variable getComputerChoice. It would be the same as writing:

if(true) {
  //do stuff
}

The condition evaluates to truthy, so the code inside the if block is executed. In your case that code is:

So, you get paper every time.

Hint

Here’s an example similar to what you need to do:

const someNumber = Math.floor(Math.random() * 10); 

if(someNumber === 5) {
  console.log('Five');
}
1 Like

does this still apply if it’s a function instead of a variable when I change the code to
`function getComputerChoice() = Math.floor(Math.random() * 3)’

I get an error saying that = is an unexpected token

A function contains code that can be repeated by calling the function. The code to generate the random number should be inside the function. A variable should be assigned to the random number. Updating the code in my previous hint, your function should have a structure similar to the following.

function fiveOrNot() {
  const someNumber = Math.floor(Math.random() * 10); //generate a random number (0-9), and assign a variable to it

  if(someNumber === 5) { //use the strict comparison operator to determine if the random number is 5
    return 'Five'; //return this if someNumber === 5 is true
  } else {
    return 'Not Five'; //return this if someNumber === 5 is false
  }
}

console.log(fiveOrNot()); //call the function and print the returned value to the console

Now I’m following! I didn’t realize about assigning a variable within the function. I adjusted my code and got it working so far, thank you!

1 Like

Cool! You’re welcome. Happy coding!

1 Like