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}.`);

Yes, that’s what I meant.

1 Like