How does Magic Eight Ball work?

Hello! I was able to successfully complete this project, but I’m having difficulty comprehending how it is that the console.log(eightBall) gives me the answer from the flow below, if I haven’t explicitly connected the two. Is it just because we left the string empty so it would always go ahead and apply the logic to whichever string was most recently empty?

Could someone shed some light for me? Thanks!

let userName = ‘’;

userName ? console.log(Hello, ${userName}!)
: console.log(‘Hello!’);

const userQuestion = ‘How are you?’;
console.log(userQuestion);

let randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;

switch (randomNumber) {
case 1:
console.log(‘It is certain’);
break;
case 2:
console.log(‘It is decidedly so.’);
break;
case 3:
console.log(‘Reply hazy try again.’);
break;
case 4:
console.log(‘Cannot predict now.’);
break;
case 5:
console.log(‘Do not count on it.’);
break;
case 6:
console.log(‘My sources say no.’);
break;
case 7:
console.log(‘Outlook not so good.’);
break;
case 8:
console.log(‘Signs point to yes.’);
break;
default:
console.log(‘Try again.’);
break;
}

console.log(randomNumber);
console.log(eightBall);

Hi,
Your ‘console.log(eightBall);’ isn’t printing anything, it’s not been changed from ‘’.
The answer is being printed out within the switch statement.

Your program will first go through the name stuff,
then generate your randomNumber and set eightBall to an empty string.

Then the switch statement will code block based on your value of randomNumber,
There is where it’ll print out ‘it is certain’, ‘it is decidedly so’, etc.

Then it’ll print out the randomNumber and then eightBall - which is still an empty string.

To connect the two you probably want to alter the case sections to assign the value instead of logging to the console.
e.g.

case 1:
eightBall = ‘It is certain’;
break;

so that its value gets changed.

Hope that helps

2 Likes

Hugely helpful, thanks so much!

1 Like

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