# If else statement

Please some change this switch statement to If else. Thanks

const randomNumbers = Math.floor(Math.random() * 8);switch(randomNumbers){

switch(randomNumbers)
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;
``````

}

console.log(eightBall)

Hello @shahanjrp, welcome to the forums! Unfortunately we can’t just give away answers on the forum, but if you have a specific question about turning `switch` blocks into `if…else` blocks, we could answer that.

In general, when you turn a `switch` block into an `if…else` block, each `case` becomes its own `else if` condition, like so:

``````//this is the switch block I want to turn into an if…else if…else block:
let aNumber = 5;
switch (aNumber) {
case 1:
console.log("1");
break;
case 2:
console.log("2!");
break;
default:
console.log("Not 1 or 2.");

//Here is the above code turned into an if…else if…else block:
if (aNumber == 1){//first case is turned into the if.
console.log("1");
}else if (aNumber == 2){//seconde case is turned into an else if.
console.log("2");
//Note all case statements after the first one get turned into else if blocks
}else{//the default
console.log("Not 1 or 2.");
}
``````

Generally, one would prefer to keep code as simple as possible, so you would generally not want to turn a `switch` block back into an `if…else` block.

I hope this helps!

Thanks a lot. its help a lot

1 Like