Magic Eight Ball

Hi evrybody, I have a question about the exercise “Magic Eight Ball”. I would like that the last line of code -console.log(eightBall)- appears only if a question is asked. I tried to add the console log in the userQuestion case, but gave me ‘error’. How to do it? Thanks a lot!

let userName = 'Jane'; userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!'); let userQuestion = ''; if (userName && userQuestion){ console.log(`${userName} asked "${userQuestion}"`); } else if (userQuestion){ console.log(`The user asked: ${userQuestion}`); } else { console.log('Ask a question!'); } 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: eightBall = 'Outlook not so good'; break; case 7: eightBall = 'Signs point to yes'; break; } console.log(eightBall);

Well, there are several ways to accomplish what you’re suggesting. Consider what it is you are trying to do. You only want to print the value that eightBall has been assigned to if a question has been asked, so one way might be to only execute your line: console.log(eightBall) if userQuestion has been assigned to something other than an empty string. See what you can come up with. Check the hint if necessary.

//something like this might work
if(userQuestion) {
} else {
  console.log("You haven't asked a question!");
1 Like

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