Yet another 8Ball Project Question

Hi all, I feel really silly asking this question. I was able to get the “case” version of this project to work fine. However, when using “else if”, I keep getting the same result, it won’t randomize. What am I doing wrong?

let userName = 'Jay'; console.log(`Hello, ${userName}!`); const userQuestion = 'Will I win the lottery?'; console.log(`${userQuestion}, such a bold thing to ask ${userName}`); let randomNumber = Math.floor(Math.random() * 8); let eightBall = ''; if (randomNumber === 0) eightBall=`It is certain`; else if (randomNumber === 1) eightBall=`It is decidedly so`; else if (randomNumber === 2) eightBall=`Reply hazy try again`; else if (randomNumber === 3) eightBall=`Cannot predict now`; else if (randomNumber === 5) eightBall=`Do not count on it`; else if (randomNumber === 6) eightBall=`My sources say no`; else if (randomNumber === 7) eightBall=`Outlook not so good`; else (randomNumber === 8) eightBall=`Signs point to yes`; console.log(`The Magic 8 Ball says, ${eightBall}.`);

If you’re going to put what the if or else if should do on a separate line, you want to use curly brackets to contain the instructions.

if (this === that) {
    thisThing = thatThing
} else if (this === other) {
    thisThing = otherThing
} else {
    console.log('What is this?')
}
2 Likes

If there is only one statement, then the curly braces can be omitted in JavaScript.
But to avoid any mistakes, it is a good idea to include curly braces as advised by javaace96747.

The problem lies in:

else (randomNumber === 8) 
  eightBall='Signs point to yes';

There should be conditions after if and else if, but there should be no condition after else.
It should work if you edit the above to,

else 
  eightBall='Signs point to yes';

Since you have included a condition after else, so your code is being interpreted as:

# Your code:
else (randomNumber === 8) 
  eightBall='Signs point to yes';

# Interpreted as: 
else {
    (randomNumber === 8)  
} 
eightBall='Signs point to yes';

Since you have omitted curly braces, so the single statement (randomNumber === 8) is considered as being part of the else block. The statement just makes a comparison and then does nothing with the result.
The statement that follows, eightBall='Signs point to yes'; is not considered to be a part of the else block. Rather, it is interpreted as lying outside the if/else-if/else statement. No matter which if/else-if condition was true, the final statement eightBall='Signs point to yes'; overwrites the string assigned to eightBall and you always get the same result.

Had you used curly braces,

else (randomNumber === 8) { 
  eightBall='Signs point to yes';
}

a SyntaxError would have alerted you to the fact that there shouldn’t be a condition after the else keyword. Alternatively, you could keep the condition but delete else and replace it with else if.

To sum up,

  • If you delete (randomNumber === 8) after the else keyword, your code will work even without curly braces (Alternatively you could keep the condition but amend else to else if).

  • Even when there is a single statement in a block, it is generally a good idea to use curly braces.

  • You don’t have a else-if condition for (randomNumber === 4). Code is still valid without it. Just mentioning it anyway. Also, your randomNumber will be in the range 0-7. It won’t be 8 because of Math.floor, so 'Signs point to yes' will never be printed.

1 Like

Thank you both! I must have needed more caffeine yesterday because I totally didn’t notice me skipping over randomNumber === 4…

As well, this afternoon I don’t want to tell you how long I stared at my curly brackets before I realized the debug window was telling me I don’t need ‘else if’ at the end of the statement chain (is that the right term? or is it string…) because there isn’t anything else happening! Sheesh, right in front of my nose.

edited code attached for anyone else in the future banging their head against the wall!

let userName = 'Jay'; console.log(`Hello, ${userName}!`); const userQuestion = 'Did I fix my mistake?'; console.log(`"${userQuestion}"; Such a bold thing to ask ${userName}....`); let randomNumber = Math.floor(Math.random() * 8); let eightBall = ''; if (randomNumber === 0) { eightBall=`It is certain`; } else if (randomNumber === 1) { eightBall=`It is decidedly so`; } else if (randomNumber === 2) { eightBall=`Reply hazy try again`; } else if (randomNumber === 3) { eightBall=`Cannot predict now`; } else if (randomNumber === 4) { eightBall=`Do not count on it`; } else if (randomNumber === 5) { eightBall=`My sources say no`; } else if (randomNumber === 6) eightBall=`Outlook not so good`; { } if (randomNumber === 7) { eightBall=`Signs point to yes`; } console.log(`The Magic 8 Ball says, ${eightBall}.`);