Converted switch conditional from Magic Eight Ball project to if/else conditional and it no longer works

Hello there. As the title states, I recently worked on the Magic Eight Ball project in JavaScript and decided to convert the switch conditional to an if/else conditional. Now, instead of logging a random string to the console, only the string assigned to (randomNumber = 1) runs: ‘It is decidedly so.’

Any help understanding the problem here would be greatly appreciated!

//Input User Data Below
let userName = 'James'
let userQuestion = 'Will I be famous'

userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
console.log()
console.log(`So, you want to know the answer to: ${userQuestion}?`);

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

let eightBall = ''

console.log()

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 right 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(eightBall);

Double check the way your are comparing randomNumber to 1, 2, 3, etc.

1 Like

I did. Once again, I’d like to reiterate, any “help understanding the problem” would be greatly appreciated.

Ok. A single = is used for assigning a value to something. In JavaScript, == or === is used to compare values.

So, randomNumber = 3 assigns the value 3 to randomNumber.
randomNumber == 3 compares randomNumber’s value to 3 to see if they are equal.

That makes sense. Thank you.