Magic-eight-ball project | console.log sometimes prints out a blank answer

I’m in the process of wrapping up this project and when I print out the console.log statement at the end occassionally (randomly), it won’t print out an answer from the switch statement and it’ll be blank. I triple checked the switch statement and can’t find any syntax errors. Could someone perhaps help spot why it’s throwing out a blank answer?

const userName = '' || 'Stranger';

console.log(`Hello ${userName}!`)

const userQuestion = 'What is the answer to life?';

console.log(`Great question ${userName}! You 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);

You misspelled eightBall in one of them. Note programming is usually case sensitive when it comes to variable names.

3 Likes

My hero thank you so much!!! This was driving me insane lol

No problem!

One technique to find the bug would be to leave console logs in all the places where there is funny stuff happening. That would narrow the problem. In this case it was observable but as it scales up we have to look for more efficient tools every time.

2 Likes