JavaScript conditionals

I’m working on the “Magic Eight Ball” exercise on Java Script conditionals.
I solved the exercise with a switch statement and then tried to do it with an if/else statement, but the latter does not work, and for the life of me I can’t figure out why. I set the variable randomNumber to pick a random integer between 0 and 7 (and that works just fine), then I wrote:

let eightBall = ‘’;
if (randomNumber=0) {
eightBall=‘You will be happy forever.’;
} else if (randomNumber=1) {
eightBall=‘Blah’;
} else if (randomNumber=2) {
eightBall=‘Your life will change today.’;
} else if (randomNumber=3) {
eightBall=‘Try again.’;
} else if (randomNumber=4) {
eightBall=‘Absolutely!’;
} else if (randomNumber=5) {
eightBall=‘Your deepest fears will come true.’;
} else if (randomNumber=6) {
eightBall=‘It will all work out in your favor.’;
} else if (randomNumber=7) {
eightBall=‘You should give up.’;
}

console.log(The eight ball answered: ${eightBall});


But no matter what the randomNumber is, when I run this code I always get ‘Blah’ on the console. Why?

The whole code is this:

let userName = ‘Joan’;
//greeting
userName ? console.log(Hello, ${userName}!) : console.log(‘Hello!’);
//Question
let userQuestion = ‘Will I get a good job?’;
//Prints question
userName ? console.log(${userName} asks: ${userQuestion}) : console.log(The question is: ${userQuestion});
//Get random number from 0 to 7.
let randomNumber = Math.floor(Math.random() * 8);
console.log(randomNumber);
//create variable for the eightBall answer.
let eightBall = ‘’;
/switch-statement control flow to get eightball answer based on random number.
switch (randomNumber) {
case randomNumber=0:
eightBall=‘You will be happy forever.’;
break;
case randomNumber=1:
eightBall=‘You already know the answer.’;
break;
case randomNumber=2:
eightBall=‘Your life will change today.’;
break;
case randomNumber=3:
eightBall=‘Try again.’;
break;
case randomNumber=4:
eightBall=‘Absolutely!’;
break;
case randomNumber=5:
eightBall=‘Your deepest fears will come true.’;
break;
case randomNumber=6:
eightBall=‘It will all work out in your favor.’;
break;
case randomNumber=7:
eightBall=‘You should give up.’;
break;
}
/

//control flow to get eightBall answer
if (randomNumber=0) {
eightBall=‘You will be happy forever.’;
} else if (randomNumber=1) {
eightBall=‘Blah’;
} else if (randomNumber=2) {
eightBall=‘Your life will change today.’;
} else if (randomNumber=3) {
eightBall=‘Try again.’;
} else if (randomNumber=4) {
eightBall=‘Absolutely!’;
} else if (randomNumber=5) {
eightBall=‘Your deepest fears will come true.’;
} else if (randomNumber=6) {
eightBall=‘It will all work out in your favor.’;
} else if (randomNumber=7) {
eightBall=‘You should give up.’;
}
//print eightBall answer
console.log(The eight ball answered: ${eightBall});

here:

if (randomNumber=0)

you use the assign operator (=), which means zero is assigned to randomNumber. Which means the condition then will evaluate whether 0 is truthy or falsy.

Shouldn’t you check if the variable is equal to randomNumber? Remember comparison operator? This might help:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

2 Likes