Magic Eight Ball Project

I was doing the project, writing a switch function that will change the variable of the eightBall based on the random number. The switch code is as below:
switch (randomNumber) {
case 0:
eightBall =‘Do not count on it’;
break;
case 1:
eightBall = ‘My sources say no’;
break;
case 2:
eightBall =‘Outlook not so good’;
break;
case 3:
ceightBall =‘Reply hazy, try again’;
break;
case 4:
eightBall =‘Cannot predict now’;
break;
case 5:
eightBall =‘Signs point to yes’;
break;
case 6:
eightBall =‘It is decidedly so’;
break;
case 7:
eightBall =‘It is certain’;
break;
default:
eightBall =‘You gotta go again, got no idea’;
break;
}

console.log(The answer is: ${eightBall});

However, sometimes this prints 'The answer is: ’
i.e. there will be no value for the eightBall. I cannot seem to figure out why this is the case. Could you help with this?

let username = ''; // prints out Hello, adding the username if it is truthy username ? console.log(`Hello', ${username}!`) : console.log('Hello!'); const userQuestion = 'Am I likely to read tonight?'; username ? console.log(`${username}, you asked \'${userQuestion}\'`) : console.log(`You asked, \'${userQuestion}\'`); let randomNumber = Math.floor(Math.random()*8); let eightBall = ''; switch (randomNumber) { case 0: eightBall ='Do not count on it'; break; case 1: eightBall = 'My sources say no'; break; case 2: eightBall ='Outlook not so good'; break; case 3: ceightBall ='Reply hazy, try again'; break; case 4: eightBall ='Cannot predict now'; break; case 5: eightBall ='Signs point to yes'; break; case 6: eightBall ='It is decidedly so'; break; case 7: eightBall ='It is certain'; break; default: eightBall ='You gotta go again, got no idea'; break; } console.log(`The answer is: ${eightBall}`);
case 3:
    ceightBall ='Reply hazy, try again';
    break;

Log everything you can. For example this:

console.log(randomNumber, eightBall, `The answer is: ${eightBall}`);

Then you would see that the empty string occurs when randomNumber is 3. That helps narrowing down the problem.

2 Likes

amazing advice, thank you so much!

1 Like