Magic Eight Ball Project Help


I just completed the magic eight ball project on the full stack developer course. I used a switch statement to randomise the answers. My issue is that when I run the code, sometimes it will randomly print an empty string, which is not ideal. Is there anything I can do to be able to ignore the empty string when printing to the console? My code is attached below.

let userName = 'Steve'; userName ? console.log(`Hello ${userName}`) : console.log('Hello'); let userQuestion = 'will I become a warewolf tonight'; console.log(`${userName} wants to know, ${userQuestion}?`); let 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 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: eighBall = 'Outlook not so good'; break; case 7: eightBall = 'Signs point to yes'; break; } console.log(eightBall);
  case 6: 
  eighBall = 'Outlook not so good';


Ternary expressions can be logged…

console.log(userName ? `Hello, ${userName}` : `Hello.`)

Much simpler, and easier to read and debug.

1 Like

Thanks for the spellcheck and the advice. I appreciate both.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.