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’;
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);

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:
    https://discuss.codecademy.com/t/how-to-format-code-in-posts/51139

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

1 Like