Magic 8 Ball

I am having trouble with this exercise, everything is running, but the answer is not following the question, as well with the response from the eight ball. Please help.

let userName = “Kaos”;

userName ? console.log(Hello ${userName}) : console.log(“Hello!”);

let randomNumber2 = Math.floor(Math.random() * 3);

let userQuestion = ‘’;

switch (randomNumber2) {
case 0:
console.log(“Will I be rich and famous”);
break;

case 1:
console.log(“Will get that promotion”);
break;

case 2:
console.log(“Will I get a position in cybersecurity.”);
break;

default:
console.log(“Just give me an answer, you know what I am thinking”);
}

console.log(${userName} has asked: ${userQuestion});

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

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 not so good”);
} else if (randomNumber === 7) {
console.log(“Signs point to yes”);
} else {
console.log(“Sorry come back later”);
}

console.log(The Magic 8 Ball says, ${eightBall}.);

To preserve code formatting in forum posts, see: [How to] Format code in posts

You have declared and initialized userQuestion as the empty string '', but in your switch statement, you are simply printing strings. Instead, you may want to assign the strings to the userQuestion variable.

// You wrote:
case 0:
    console.log("Will I be rich and famous");
    break;

// Consider changing to:
case 0:
    userQuestion = "Will I be rich and famous";
    break;

Similarly, you have declared and initialized eightBall as an empty string '', but in your later code, you aren’t assigning any values to this variable.

// You wrote:
if (randomNumber === 0) {
    console.log("It is certain");
} else if ... 

// Consider changing to:
if (randomNumber === 0) {
    eightBall = "It is certain";
} else if ... 
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.