JavaScript-- Rock, Paper Scissors Project https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors-javascript

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

In the video walk through there is no break; after the code block following case when setting up the switch statement. So my question is when do I use break and how do I know if it’s not needed?

In steps 8-10 we set up conditional statements. The code block of each if statement is another if statement. An if statement nested in an if statement. Why don’t we use && operator? If we’re asking if a user’s choice in this game is scissors is the answer to that question a boolean value? This is the first time that I’ve seen conditionals nested in each other so far. When do we use this format and when do we use && and what’s the difference?

Thanks!

Good question. I found some explanation here.
It’s much like using a break statement in a loop.

Quoting it here:
When JavaScript reaches a break keyword, it breaks out of the switch block.

This will stop the execution of inside the block.

It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.

Also, here’s a good explanation on nested ‘if’ statements:

The use of && would be useful if you required two conditions to be true before you went deeper into the nested ‘if’.
if (person === 16 && personPassesTest === true) {
if (personIsOrganDonor === true) {
return new license with organ donor sticker
} else {
return new license
}
} else {
return “You need to wait til your 16 then take the test”
}

2 Likes

why do i get undefined ? what’s wrong?

I’m on step 4 of the project, trying to see if the if else statement in the function works. However, when I click save after the else statement, I’m receiving an error. Below is my code so far. Anyone see something I’m not?

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

You have semi-colon here:

‘scissors’);{

This is ending the line, and breaking your if...else statement off after the filter. I took that semi-colon out and it ran.

Hello,

I’m also trying the else if statement for this project and Math.random is always returning paper. Any ideas? here’s the code:

const getComputerChoice = () => {

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

if (randomNumber = 0) {

return ‘The computer chooses rock’;
}

else if(randomNumber = 1) {

return ‘The computer chooses paper’;

}

else if(randomNumber = 2) {

return ‘The computer chooses scissors’;

}
};

console.log(getComputerChoice());

Hi, hope all is good. How did you embed the code like that? :slight_smile:

You are using if (randomNumber = 0) with only a single =.
If you swap this to triple === it will work