Magic 8-ball using if...else if

Hello, Im working on Magic 8 ball project and was able to sucessfully complete using switch statement. I wanted to try by using else if. Would I just take the lst console.log out and write that in each else if line? Like should it be?
else if (randomNumber === 7){
console.log(‘The Magic 8 ball says, Signs point to yes’);

let userName = "Mitch";
userName ? console.log(`Hello, ${userName} !`) : console.log("Hello!");

let userQuestion = "Will I win the lottery?";

console.log(userQuestion);

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

let eightBall = "";
if (randomNumber === 0) {
  console.log("It is certain");
} else if (randomNumber === 1) {
  console.log("It is decidedly so");
} else if (randomNumber === 2) {
  console.log("Reply hazy try again");
} else if (randomNumber === 3) {
  console.log("Cannot predict now");
} else if (randomNumber === 4) {
  console.log("Do not count on it");
} else if (randomNumber === 5) {
  console.log("My sources say no");
} else if (randomNumber === 6){
  console.log('Outlook not so good');
} else if (randomNumber === 7){
  console.log('Signs point to yes');
}
console.log(`The Magic 8 ball says, ${eightBall}.`);


let userName = 'Mitch';
userName ? console.log(`Hello, ${userName} !`) : console.log('Hello!');

let userQuestion = "Will I win the lottery?";

console.log(userQuestion);

let randomNumber = Math.floor(Math.random() * 8);
console.log(randomNumber);

let eightBall = '';

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;
  case 4:
    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";
    break;
}

Yes, that works (except for the last console.log that has eightBall in it).
You could also do something like

else if (randomNumber === 7){
  eightBall = "The Magic 8 ball says, Signs point to yes");

and you could have this all the way at the end (outside the if and else-if stuff)

console.log(`The Magic 8 ball says, ${eightBall}.`);
1 Like

So I would change each else if to drop console.log and just use eightBall =
Im not clear what you said “except for last console.log”

Is this what you meant?

let userName = "Mitch";
userName ? console.log(`Hello, ${userName} !`) : console.log("Hello!");

let userQuestion = "Will I win the lottery?";

console.log(userQuestion);

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";
} else if (randomNumber === 7) {
  eightBall = "Signs point to yes";
}
console.log(`The Magic 8 ball says, ${eightBall}.`);

Hi there,

It’s my first post in here!
Been redoing that particular project for a couple of days as I’ve been struggling with it. The answer “It is certain” never comes up (randomNumber === 0) - although I have checked that my code is correct and identical to the one posted above. I was wondering if it was because of the Math.floor function? Am I doing anything wrong?

Copy paste your code. That may offer some clues as to what is amiss.

Here it is:

(```
let userName = ‘Mum’;
userName ? console.log(Hello, ${userName}!) : console.log(‘Hello!’);
const userQuestion = ‘Is the world going to end soon?’;{
console.log(${userName} asked: ${userQuestion})
}
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;
if (randomNumber === 0) {
eighBall = ‘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’;
}
console.log(The Magic 8 Ball says: ${eightBall}.);

Typo in variable name

// Your code:
eighBall = 'It is certain';

// It should be:
eightBall = 'It is certain';

:flushed: very ashamed that I ended up reviving this thread because of a simple typo…

Many thanks for your keen eye and apologies!!