Need help with my code with if and else statement

let userName = “Omer”;

userName ? console.log(Hello, ${userName}! ) : console.log(“Hello!”);

const userQuestion = “How many years until I unlock limitless?”;

console.log(userQuestion);

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

let eightBall = 200;

if ((eightBall = 0)) {

console.log(“It is certain”);

} else if ((eightBall = 1)) {

console.log(“It is decidedly so”);

} else {

console.log(“enter a valid number”);

}

My code keeps printing “It is decidedly so” instead of the other outputs?

Thanks!!

This is an assignment, not a test:

if ((eightBall = 0)) {

This is a test:

if ((eightBall == 0)) {

The assignment happens, then the test happens. The test is then then value after the test.

It’s a little confusing, but you can see this in action:

let eightBall = 200;
console.log({ eightBall });
console.log({ "(eightBall = 0)": (eightBall = 0) });
console.log({ eightBall });
console.log({ "(eightBall = 1)": (eightBall = 1) });
console.log({ eightBall });

Output:

{ eightBall: 200 }
{ '(eightBall = 0)': 0 }
{ eightBall: 0 }
{ '(eightBall = 1)': 1 }
{ eightBall: 1 }

Note, you can protect yourself from this by using const whenever possible.

const eightBall = 200;
console.log({ eightBall });
console.log({ "(eightBall = 0)": (eightBall = 0) });

Output:

{ eightBall: 200 }
test.js:3
console.log({ "(eightBall = 0)": (eightBall = 0) });
                                            ^

TypeError: Assignment to constant variable.

thanks! appreciate the help, yes it was an assignment that was easier with switch but wanted to try the if else way too