Why does = always give the same response in my code block? (from the magic eight ball project)


Whenever I run the codeblock below, the output is always ‘Boo!’. Why?

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

var eightBall = ‘’;

if(randomNumber = 1) {eightBall = ‘Boo!’}

else {eightBall = ‘try again’}


Because you are using the assignment operator not the equality one


let a = 5;


if (a === 5) {
  \\ do this
1 Like

Thanks for your help.
Should I change the assignment operator to the equality operator on the first line (randomNumber = Math.floor…) ?
In this case, if I use the assignment operator inside the conditional operator if, does that automatically update randomNumber to always be 1?

No, you want that to be assigning a random num to the variable