Help with Magic Eight Ball if/else test

Hi everyone, I’m completing the Magic Eight Ball project and while I had no trouble getting the switch statement to work, I cannot seem to get the same results with the if/else statement. It only runs the first response (‘It is certain’) and none of the others. Can someone guide me to what I’m doing wrong? Thank you!

let userName = "Tam";
let userQuestion = "Do you think I'll pass my coding assessment?";
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = "";

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

console.log(`${userName} has asked: ${userQuestion}`);
console.log(`The magic eight ball says: ${eightBall}`);

if (randomNumber) {
  console.log("It is certain");
} else if (randomNumber) {
  console.log("It is decidedly so");
} else if (randomNumber) {
  console.log("Reply hazy try again");
} else if (randomNumber) {
  console.log("Cannot predict now");
} else if (randomNumber) {
  console.log("Do not count on it");
} else if (randomNumber) {
  console.log("My sources say no");
} else if (randomNumber) {
  console.log("Outlook not so good");
} else if (randomNumber) {
  console.log("Signs point to yes");
}
1 Like

You have the very same condition in each of your else if statements. So it always executes the code of the first condition that evaluates to truthy. And if the conditions are all the same that will always be the first one.
Additionally, your condition only checks if the variable randomNumber exists and if it has a value – which is the case.

Thank you for your help! I’m going to review the lesson again and start the project over.

1 Like

I got it this time! I kept getting stuck in assigning the numbers 0-7 to each randomNumber expression in the if/else if statements because I thought doing so would somehow not make the results random at all, if that makes sense, lol. A good reminder to take my time and trust the code :slight_smile: Thanks again.

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

let userQuestion = "Do you think I'll pass my coding assessment?";
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(`The magic eight ball says: ${eightBall}`);
2 Likes

That would have indeed been the case, if you had assigned the numbers to the the variable randomNumber in the if/else statements. That would have looked like this then:

if (randomNumber = 0) // assignment

What you did is comparing the value stored in the variable randomNumber to the number:

if (randomNumber === 0) // comparison
=== // strict comparison operator ('1' === 1 is false)
== // comparison operator ('1' == 1 is true)
= // assignment operator 
2 Likes

Exactly! Thank you for that follow up explanation, that’s helpful to know also.

1 Like