Question about Magic Eight Ball project- Result printed out in console.log

I have been stucked in the Magic Eight Ball project and I had referred to the Project Walkthrough video provided. However, I still have parts that I don’t understand.

  1. Why do we need let eightBall = ‘’ ?
  2. Why is the result printed on console.log(eightBall) keep changing whenever I click “Save”?

Following is my code:

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

let userQuestion = ‘who is the richest in the world.’;
console.log(The user asked: ${userQuestion});

const randomNumber = Math.floor(Math.random() * 8);

let eightBall = ‘’;
switch (randomNumber){
case 0:
eightBall = ‘It is certain’;

case 1:
eightBall = ‘It is decidedly so’;

case 2:
eightBall = ‘Reply hazy try again’

case 3:
eightBall = ‘Cannot predict now’

case 4:
eightBall = ‘Do not count on it’

case 5:
eightBall = ‘My sources say no’

case 6:
eightBall = ‘Outlook not so good’

case 7:
eightBall = ‘Signs point to yes’


Hi @chlee7554331689
Welcome to the forum

In your switch statements, you assign a value to the variable eightBall. You need to declare each variable you use with either the var, let, or const keyword. You could prepend ‘let’ in each switch statement instead, but that would be tedious and not a good practice.

Because each time you run your program, this

returns a new result.

  1. Please make it easier for us to debug your code by formatting it correctly:

Thank you very much for your reply!
It clears my doubts now :slight_smile:

1 Like