Magic Eight Ball Explanation

Can someone please explain in this practice how eightBall connects to randomNumber? I understand that randomNumber gives us the numbers we are using in our switch statement, and the result of eightBall is what we are printing to the console… but in what part of the code and how are we connecting randomNumber to eightBall?



It’s within the switch codeblock. It matches up randomNumber with the ‘case’ that matches (or default if there isn’t one) and runs the bit underneath it until it reaches break - where it ends the switch statement.

switch (randomNumber){ case 0: eightBall = 'It is certain'; break; case 1: eightBall = 'It is decidedly so'; break; case 2: eightBall = 'Reply hazy. try again'; break; case 3: eightBall = 'Cannot predict now'; break;

So, for the above snippet, say randomNumber was 2. The switch statement would send it to case 2, where it would change eightBall to ‘Reply hazy, try again’. The next line is break, which means it would end the switch statement and go on with the rest of the code.

The switch() statement matches the argument (an expression) with the case clauses (also expressions) to determine which branch to follow.

switch (expression) {
case expression1: // branch statement; break;
case expression2: // branch statement; break;
default: // default branch

Be sure you have completed all the lessons and exercises leading up to this project. There is nothing to be gained by skipping them. If we can assume you have arrived here after completing everything so far, then it would indicate some review is in order.

This helps! It looks like I mistakenly took a shortcut and console logged each result under the cases, rather than linking it back to the value of eightBall. Thank you both for your responses!