Problem with Objective - "Magic Eight Ball"

Hello guys!

I’ve been advancing with my JavaScript basic course, but here I am, stucked in one particular project which I can’t solve. The problem itself relapses in that I want to do the same code in “else-if” clasue as I did correctly in “switches” (in which one I did not have problems at all to find the right solution).

I want to know if you guys could help me or guide me finding my mistake. Here is the code:

let userName =‘Albes’;
userName ? console.log(Hello, ${userName}!) : console.log (‘Hello!’);

let userQuestion = “Will I win the lottery soon?”;

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

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

let eightBall = ‘’;

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

console.log(Magic Eight Ball’s says, ${eightBall});

**** At the end, it prints this following code:

Hello, Albes!
Albes has asked - Will I win the lottery soon?
Magic Eight Ball’s says,

After the says, on my “switch” code it runs perfectly, obtaining the different results randomly. But I can’t find the error on my “else-if”.

Thanks guys!!!

As per your code, randomNumber is a random number between 0 and 7 (inclusive).

You have initialized eightBall as the empty string.

In your else if conditions, you are checking eightBall (an empty string) against the numbers 0 to 7. Since none of the conditions evaluates as true, so the original value of eightBall (empty string) remains unchanged which is then printed by the console.log statement at the end.

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

The following should work:

let userName ="Albes";
userName ? console.log(`Hello, ${userName}!`) : console.log ("Hello!");

let userQuestion = "Will I win the lottery soon?";

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

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

let eightBall = "";

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

console.log(`Magic Eight Ball"s says, ${eightBall}`);

Thanks my man! I solved it by myself but I just saw this and it is pretty good explained if anyone want to check it out!

Appreciate the effort :slight_smile: