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