Converting switch to If...else

I created this block of code for the Magic Eight Ball project I used a switch statement instead of If…else…if.

const 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 6:
eightBall= ‘My sources say no’;
break;
case 7:
eightBall = ‘Outlook not so good’;
break;
case 8:
eightBall = ‘Signs point to yes’;
break;
}
console.log(Magic Eight Ball says ${eightBall});

The instruction suggest that I should convert the switch to an if…else…if statement. I have tried a couple of ways but I keep getting a syntax error and I’m not sure what I’m doing wrong.

What I can’t figure out is how to use both variables in the if statement. I would appreciate if someone could show me how to write the statement.

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-control-flow/projects/magic-eight-ball

Think of each case as an if conditional.

Hi jaipher,

This worked for me on Step 9.

  • If you started with a switch statement, convert it to if/else if/else statements. -

I tried over and over again and finally used the ==.
Here’s my code below!

Good luck,
Nancy

let userName = ‘Jane’;

userName ? console.log(Hello ${userName}!) : console.log(‘Hello!’);

const userQuestion = ‘Will I become a mermaid this summer?’;
console.log(The user asked: ${userQuestion});
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;

if (randomNumber == 0) {
console.log(‘The eight ball answered: It is certain’);
} else if (randomNumber == 1) {
console.log(‘The eight ball answered: It is decidedly so’);
} else if (randomNumber == 2) {
console.log(‘The eight ball answered: Reply hazy try again’);
} else if (randomNumber == 3) {
console.log(‘The eight ball answered: Cannot predict now’);
} else if (randomNumber == 4) {
console.log(‘The eight ball answered: Do not count on it’);
} else if (randomNumber == 5) {
console.log(‘The eight ball answered: My sources say no’);
} else if (randomNumber === 6) {
console.log(‘The eight ball answered: Outlook not so good’);
} else if (randomNumber == 7) {
console.log(‘The eight ball answered: Signs point to yes’);
} else {
console.log(‘The eight ball is broken’)
};