Magic eight ball

Can someone please check this code for Magic Eight Ball.

let userName = “”;
//player name
userName ? console.log(Hello, ${userName}!) : console.log(“Hello!”);
let userQuestion = ‘’;
//player question
console.log(${userName} has asked - ${userQuestion});
let 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 says no”;
case “6”:
eightBall = “Outlook not so good”;
case “7”:
eightBall = “Signs point to yes”;
console.log(Magic 8 Ball says, ${randomNumber}.);

I can’t get it to work. :frowning:

Hello, @method3829896424. Welcome to the forums. It would be very helpful going forward if you post your code with it’s original formatting intact. See, How do I format code in my posts?

It would also be helpful if you elaborate a little here. What specifically doesn’t work?

Hi, sorry. Newbie. Whenever I type the last console.log it doesn’t choose between the cases. It just doesn’t show anything when I type in, console.log(Magic 8 Ball says, ${eightBall}.)

Consider this line:

What is the type of the value assigned? Is it a string, a number or a bool?

Then consider your case statements. For example:

If you still need help, please post your current code, but make sure it is formatted.

let userName = ‘’;
// the name of the person asking the question
userName ? console.log(Hello, ${userName}!) : console.log(‘Hello!’);
// the question the person wants to ask
let userQuestion = ‘’;
console.log(userName + ', you asked ’ + userQuestion + ', and the answer is ');
// random number generator
let randomNumber = Math.floor(Math.random() * 8);
// different possible responses to the question asked based on random number generated
let eightBall = ‘’;

if (randomNumber = 0) {
console.log(‘It is certain’);
} else if (randomNumber = 1) {
console.log(‘It is decidedly so’);
} else if (randomNumber = 2) {
console.log(‘Reply hazy try again’);
} else if (randomNumber = 3) {
console.log(‘Cannot predict now’);
} else if (randomNumber = 4) {
console.log(‘Do not count on it’);
} else if (randomNumber = 5) {
console.log(‘My sources say no’);
} else if (randomNumber = 6) {
console.log(‘Outlook no so good’);
} else {
console.log(‘Signs point to yes’);

the code written worked just fine, my question is how do implement into this in order to have direct interaction with the user? When i try and use prompt() in any way thats exampled in the book thats part of this lesson it always errors out. I was reading that its because this isnt an actual webpage environment… is there any way to make it work on this platform?

I found the error
Since randomNumber is a number the case should not have “” since this is a string value. Thank you.

1 Like