Converting switch to If...else


#1

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


#2

Think of each case as an if conditional.