Magic Eight Ball: which type of conditional statement to use? (potential spoilers)

The if-else block occupies fewer lines, but the switch statements appear easier to read. Which option is superior, if any, from a technical standpoint?

Also, can anyone nitpick my code? :slight_smile:

// Choose a random number from 0-7 const randomNumber = Math.floor(Math.random() * 8); // Prepare to store 8-Ball message let eightBall; // Pick from available 8-Ball messages: // switch statements 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; } // if-else statements /* 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'; } */ // Output 8-Ball message in all caps console.log(`The Magic 8-Ball says:\n${eightBall.toUpperCase()}.`);

I often find easier to read more important and happily accept that might take up a couple more lines

superior is such a strong word, I think switch is preferable/better.

1 Like