Error in if-else statement

Conditional If-Else statement is not working.

Project_link : https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-control-flow/projects/magic-eight-ball-1

I have used the following code here for the conditional, and the only output I am getting is "“It is certain” which is the condition defined when the randomNumber = 1. I have tried using Switch and switch works perfectly fine.

Following is code:

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

Can anyone help me solve this? Where my code is going wrong?

Hey there @divyeshpanchal, welcome back to the forums!

Remember that the assignment operator ( = ) isn’t the same as the equality operator ( == ) nor the strict equality operator ( === ).

So for your conditions, which one should you use?

3 Likes

So, should I use (==) or (===) , in the following spaces?

If you’re trying to see if a variable equals something (e.g. randomNumber is equal to 1) you’ll want to use ===. If you’re setting a variable equal to something you’ll use = .

if (randomNumber === 1) 

It seems you are using the assignment operator (=), which is used to declare or reassign a variable. Instead use (==) equality operator or (===) strict equality operator. Which checks to see if conditions meet requirements either by data type or not. i.e. 3 == ‘3’ is correct, 3 === ‘3’ results in a error.

Also a useful tip, it’s gonna to be beneficial to use a switch statement instead of if statements if you have a lot of conditions to check. With switch once the condition is met the switch stops executing and moves on to the next part of the script. Making your javascript faster, have better performance, and better for the memory.
With if statement it will continue to execute even if the condition is met until it has run through all conditional statements.

1 Like

It seems perfect to me. I feel the problem is in how you call it. For example;

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

This is how to call it properly. Notice that because it is outside the block of code that is inside your randomNumber(8) function, I still used else if at the end of the if-else function, so as to reference the number 8 in the parentheses. Paste this piece of code into your work and see if it works.
N.B: This is an eight ball, it should have eight possible outcomes.