Can get answer with switch statement but not else if (Javascript)

I’m working on the below javscript excercise:

https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1

I have been able to use a switch statement and i can get multiple answers for the magic eight ball

When i use and else if statement i keep getting the same answer even though the number generated keeps changing.

Any help please, would be much appreciated:

let userName = (‘Jane’);

userName.length > 0?

console.log(Hello ${userName}):

console.log (‘Hello’);

let userQuestion = ‘Should I leave?’;

console.log(${userQuestion} ${userName});

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

console.log(randomNumber)

let eightBall = (’’);

switch (randomNumber) {

case 0:

eightBall = 'It is certain';

break;

case 1:

eightBall = 'It is decidedly so';

break;

case 2:

eightBall = 'Reply hazy try again';

break;

case 3:

eightBall = 'Cannot predict now';

break;

case 4:

eightBall = 'Do not count on it'

break;

case 5:

eightBall = 'My sources say no'

break;

case 6:

eightBall = 'Outlook not so good';

break;

case 7:

eightBall = ‘Signs point to yes’;

break;

}

console.log(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 {

eightBall = ‘Signs point to yes’};

console.log(eightBall)

From quick glance, you need to be using the ‘strict equals’ comparison operator:

eg if (randomNumber === 5)

1 Like

Hey,
First of all, = is for assignment like let name = 'Timothy'. You need to be using === for direct comparison. As to why you’re getting the same answer, I assume you are saying that you will get two of the same sentence printed in your console but the sentence is not always the same. This is because one value will be coming from your switch comparisons and the other one will be from your if and else if code. They are running off the same random number. That number has not changed and it’s going to be the same one that your switch picked up hence why you should be getting the same sentences. Only once you run your code again will you get another random number. Hope this helps. If this isn’t what you meant then I’d be happy to try answer again but a piece of advice is to copy and paste your code, select it and then press on the < / > button to make it easier to read :slight_smile:

To summarise, imagine the random number generated is 2. case 2 runs and eightBall = 'Reply hazy try again'. The if and else if’s use the same number, so let’s have a look at what happens when 2 is picked up.

else if (randomNumber = 2) {
eightBall = ‘Reply hazy try again’;
}

See? They’re the same because they’re using the same randomly generated number :slight_smile:

(EDIT: I now realise I may have completely mis-read your question. My apologies)

Thanks both. Ok will do the </> next time I have a question.

I’m still trying to figure out when I use = and when I use === so that’s really helpful!

And it now works so thanks.

What I’m slightly confused is why, when I used the if/else statement, it logged the value for
else if (randomNumber = 1)

It always logged ‘It is decidedly so’

Do you know why this would happen?

Yes I do understand that same sentences as the same random number is generated. I just wanted to use both types of statement to compare - but thanks :slight_smile:

If you could copy your code and put it in < / > I could have a look and run it to see why that’s happening.

Great thanks!

let userName = ('Jane');
userName.length > 0? 
console.log(`Hello ${userName}`):
console.log ('Hello');

let userQuestion = 'Should I leave?';

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

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

console.log(randomNumber)

let eightBall = '';

console.log(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 {
eightBall = 'Signs point to yes'};

console.log(eightBall)

The reason is that happens is because randomNumber = 1 sets the randomNumber variable to 1. 1 is a truthy value, which means that

evaluates to true, and so the code in that condition is run.

1 Like

Ok so first of all, you should be using === instead of =. I’ll explain that later in the reply. The reason it’s printing “It’s decidedly so” is because 0 is falsey. randomNumber = 0 evaluated to falsey, so the code went to the next branch which evaluated to 1 which is truthy hence why you got 'It is decidedly so" Also, the console.log(eightBall) before your if statements does nothing since it’s got no value at that point.

As to the difference between = and ===, let’s consider these two examples:

= is used to assign. An example of this would be let name = 'Bob'. I assigned the string ‘Bob’ to the variable ‘name’.

=== is used for strict comparison. Now let’s consider this next example.

let stringNumber = '1';
let Number = 1
console.log(stringNumber === Number); // The console will output false because they are not of the same data type. One is a string and one is a number.
console.log(stringNumber == Number); // the console will output true.

Why did the == output true? Because == compared the two but converted the string into a number. Once both were numbers the code said “yeah they’re the same. Output: true”

If you still need a bit of explaining don’t hesitate to ask :slight_smile:

1 Like

Similarly, the code

if (1 == true) {
console.log("they are the same?");
}

Logs the string they are the same?, since 1 and true are both truthy values.

2 Likes

Just a little addition to this. When you run what is practically the same code but with an === instead of an ==, you get this:

if (1 === true) {
console.log("they are the same?"); // They are not the same so this does not print. 1 is a number and true is a boolean.
} else {
  console.log("They're not  the same"); // This prints because the previous if could not.
}
1 Like

Thanks so it’s the same as writing

else if (randomNumber = true)

?

Thanks, you just answered my next question!

Ok thanks just trying to get to grips with ==,

This converts strings to numbers. so the below code converts each phrase to the number it belongs to. Will probably need to practice a bit more with this to understand it better.

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

console.log(randomNumber)

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 {
eightBall == 'Signs point to yes'};

console.log(eightBall)

Not quite. What you’re saying is that you want eightBall to equal the number it’s assigned to. There’s a problem with this. Let’s take an example:

} else if (randomNumber = 3) {
eightBall == 'Cannot predict now';
}

Firstly, ‘Cannot predict now’ is a string. This string has just been mentioned for the first time ever so it’s not assigned to anything. It can be assigned to eightBall but then you need a single = because you’re assigning eightBall to ‘Cannot predict now’. Also, your code still includes a single = for randomNumber = 3 and so on. Your code should look like this. (Just a snippet of it)

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

console.log(randomNumber);

let eightBall = '';

if (randomNumber === 0) {   // We use === because we're doing a direct comparison. randomNumber is a well... number and so is 0.
eightBall = 'It is certain';  // Notice the single = ? We're assigning eightBall to the phrase.
} else if (randomNumber === 1) {
eightBall = 'It is decidedly so';
} else if (randomNumber === 2) {
eightBall = 'Reply hazy try again';
// and so on...

When using == we can do something like:

let number = '3';
console.log(3 == '3'); // output is true since the string was converted to a number.

Now let’s consider a string and a number.

let number = 'three';
console.log(3 == 'three'); // output is false

The output for the second piece of code is false because the code doesn’t actually know how to spell three. It’s not aware that 3 and three are the same thing to us.

Here’s a link to some more information on this. It helped me when I was learning this: https://www.guru99.com/difference-equality-strict-operator-javascript.html

2 Likes

OK thanks for all your help, will check it out!

1 Like

It appears to me that you need to first understand the difference between = and either == or ===.
= is nothing at all like the other two. The other two are very similar. = is the assignment operator. == and === are comparison operators. When we want to assign a name (variable) to a specific value, we use the assignment operator, =. Neither of the other two will work for assigning a value. == and === are for comparing one value to another. That is all they do.

const myValue = 5; //assignment (myValue is assigned to the integer 5)

myValue == 5; //comparison (will return true or false after attempting to coerce the values to the same type)

myValue === 5; //strict comparison (will return true or false if the values match exactly without type coercion)
1 Like