Rock paper scissors

hi guys,
I’ve been stuck on this project, on and off now for a few days. Here’s the task (no. 6), “Test the function by calling it multiple times and printing the results to the console. You can delete this when you know your function works.”
I keep editing the code, but still getting the error message. Here’s what I have so far:

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

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

I’ve not worked through all your code, but already I can see that your ‘if’ statements have an issue.

You need to use === (an equality operator) in an ‘if’ statement, not a = which just declares a variable as equal.

EDIT: OK, I’ve had a bit more of a look, and at the moment your code in the getComputerChoice function doesn’t return any values. At present your function produces a random number (0, 1 or 2), but doesn’t do anything with it. You’ll need to return what you calculate in that function:

const getComputerChoice = () => {
return Math.floor(Math.random() * 3);
}

Hi, thanks for your response.

i fixed those parts, but still getting a syntax error msg.

Hi! I don’t think you should be using a ; after the closing } of your if statements.

3 Likes

Yes, and it also looks like you have an extra } which is floating at the end of your code. It’s highlighted in your editor as red to show you that it’s dangling.

EDIT: Looking again, I think you might need to relook at the section from Line 10 to Line 22. Your getComputerChoice function ends at Line 12. Is that what you intend? How would you describe what you hope the behaviour of Lines 10-22 to be?

1 Like

I’ll put my hand up immediately and say this is pedantry, but === is the “strict equality” operator and not the equality operator (==).

If you simply want to confirm that two values are equal, you ought to use the equality operator:

// everything here is a number
2 == 2.0 // true
4 === 4.0 // true

The “strict equality” operator includes the requirement that both operands are of the same type. So, let’s say we’re working with “truthy/falsey” values and we do this:

1 == true // true
"true" == true // true
1 === true // false

Not necessarily an important difference here, perhaps, but == and === aren’t the same (and the preference for the latter is something I personally find irritating…).

That looks like the problem to me too…

1 Like

I agree with all of that, especially that it’s pedantry :wink: I can’t recall if the Codecademy course teaches the difference by this point, but I don’t think it does so I opted to just suggest the strict equality which the op has used elsewhere in their programme correctly.

Besides, given the variable type is known, why not use a strict equality? It seems that is a matter of preference. Feel free to DM me separately as I don’t think this thread is really about that question.

1 Like

@sivwri
I’m attempting to get the computer to give/print me a random number (integer) between 0-2. Each of these numbers should represent either rock, paper or scissors.

Hello @cloud4562193327. Take at look at your else statement as well. Should you have a condition after it?
The purpose of the else is to cover all other cases, so when you provide a condition, it throws an error, as you are telling the computer in all other cases, when "object" equals "condition", do the following. If you told that to a human, they’d get confused. When the computer gets confused, it throws an error.

2 Likes

I can see that this part of your programme is going to generate a number between 0 and 2:

const getComputerChoice = () => {
  return Math.floor(Math.random() * 3);
}

But do you see that the } at the end of that section actually ends the getComputerChoice function? Line 13-20 are really part of the main body of the code. You’ll need to:

  1. Remove the ; before the if at the end of line 16 and before the else in line 18.
  2. Rewrite lines 10-22 as one function. Ask yourself if you need to generate the random number twice (which it currently does). When you rewrite the function, you might end up using the dangling } in line 22, but make sure you check because at the moment it’s unused and will give you a syntax error!

Let us know how you get in, and do copy and paste your new code here if you get stuck!

1 Like