Trouble with else if clause in Magic Eight Ball project

i did the eight ball project with a switch statement, as is recommended, and it worked great. at the end it tells me to try it with an else if clause for more of a challenge, which i do. trouble is, it won’t execute the last else if block no matter what i do. here’s the code:

let userName = '';
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
let userQuestion = 'Is Tyrell real?';
console.log(`${userName} asked: ${userQuestion}.`);
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';

/*switch (randomNumber) {
  case 0:
    eightBall = 'It is certain';
    break;
  case 1:
    eightBall = 'It is decidedly so';
    break;
  case 3:
    eightBall = 'Reply hazy try again';
    break;
  case 4:
    eightBall = 'Cannot predict now';
    break;
  case 5:
    eightBall = 'Do not count on it';
    break;
  case 5:
    eightBall = 'My sources say no';
    break;
  case 6:
    eightBall = 'Outlook not so good';
    break;
  case 7:
    eightBall = 'Signs point to yes';
  case 8:
    eightBall - 'No dude';
    break;
}*/

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'; // -
} else if (randomNumber === 7) {
  eightBall = 'Signs point to yes'; // -
} else if (randomNumber === 8) {
  eightBall = 'No dude'; //
}

console.log(`The eight ball answered: ${eightBall}`);

I added a comment of “-” after every else if block that did execute, hence why the eighth one is missing one. not sure what is wrong

Hi @text7135892289
Welcome to the forum!

This returns numbers from 0 to 7. So the last condition randomNumber === 8 is never fulfilled. But neither is your case 8.

1 Like

oh my god you’re right. thats my bad, i guess i was only half paying attention when i read the explanations. although i did find it strange they only provided 7 examples. thanks so much!

1 Like