Help me spot what is wrong with my if...else statement!

[Magic Eight Ball Project] (https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-conditionals/projects/magic-eight-ball-1)

I am working on the Magic Eight Ball Project and got the program to run correctly using a switch statement but when I tried using if…else, I couldn’t get the program to run correctly. Instead of generating a random string each time I ran it, it just kept running the string ‘It is decidedly so’ over and over again. Why is this? What am I missing? I’m sure it is glaringly obvious and I’m just missing something that’s right in front of me.


let userQuestion = 'Will I get rich?';
console.log(`The user 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 = 'Don\'t 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(eightBall);

TIA!

(randomNumber = 0)
should be
(randomNumber == 0)
and so on …

Remember that = is used to assign values
and == is used to test whether things are equal

2 Likes

Thanks so much for your response! I stared at that for the longest time and could not for the life of me figure out what it was. Of course it was super simple.