Introduction to JavaScript: Magic Eight Ball Project

I’m working on the following project: https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1

I completed the project using switch and I’m trying to do it using if-else but for some reason, my code is constantly printing “It is decidedly so” despite the randomNumber variable changing. I can’t see anything wrong with my code. Do you notice anything?

let userName = "";
// Ternary expression for userName
userName ? console.log(`Hello ${userName}!`) : console.log("Hello!");

const userQuestion = "Will I buy a Mac?";
console.log(`The user asked: ${userQuestion}`);

// Generate random number between 0 and 7
let randomNumber = Math.round(Math.random()*7);
console.log(randomNumber);

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 = '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);

What affects whether or not that “clause” executes? The condition, right? So, what if you were to look at only the condition. What does the condition evaluate to for different values of randomNumber? Run it by itself and find out. And, since you’re looking at a single expression you can also consider what each thing in it does/means

What affects whether or not that “clause” executes? The condition, right?

Yes, namely the value of randomNumber.

So, what if you were to look at only the condition. What does the condition evaluate to for different values of randomNumber? Run it by itself and find out.

Are you asking me to hardcode randomNumber to 0, 2, etc. via the following:

let randomNumber = 0

I’ve done this but there is no change.

I don’t exactly understand what you’re asking me to check. Logically, I don’t see what I’ve done wrong:

  1. random number generated and stored as randomNumber
  2. condition of if-else statement is value of randomNumber
  3. value of randomNumber determines value of eightBall

You have an expression that you have demonstrated does not behave like you want.

So, delete all the rest of the code (temporarily) or use a new file, or a repl, and experiment with only the expression, by itself. See how it behaves.

randomNumber = 1

Since there is a variable there, you can replace it with a value, like 0, 1, "zebra", and see what the result is.

Usually if you want to see what a value is, what you would do is print it.

console.log("zebra" = 1) // true or false?

Additionally, there is some kind of operation you’re doing there. Which operation is that? What does that operation promise to do for you? Does that operation do something that is useful to you here?

The code works now when I replace = with ===

let userName = "";
// Ternary expression for userName
userName ? console.log(`Hello ${userName}!`) : console.log("Hello!");

const userQuestion = "Will I buy a Mac?";
console.log(`The user asked: ${userQuestion}`);

// Generate random number between 0 and 7
let randomNumber = Math.round(Math.random()*7);
console.log(randomNumber);

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 = '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);

Exactly! Consider this code:

let r = 0
if(r = 0) {
  console.log("This only prints if the condition evaluates to true. r is equal to zero, right?")
} else {
  console.log("This only prints if the condition evaluates to false.") 
}

What will print? Why?

Wasn’t my mistake confusing a comparison operator for an assignment operator?

Well my point was that you can narrow it down enough that you don’t need to treat the whole code like some grand mystery, you can find out exactly which operation didn’t behave the way you wanted, and then you can reconsider what that operation is.

You can eliminate the guesswork, the need to find the problem by accident.

1 Like

Yes, but why did 'It is decidedly so' print instead of 'It is certain'?

Yes, but why did 'It is decidedly so' print instead of 'It is certain' ?

Because the condition in the if-statement (randomNumber = 0) evaluated to false so it treated the next else-if statement as the else-statement?

It did not evaluate to false. What did it evaluate to? (try it)
And the next one did not evaluate to true.

What did it do?
What is the result of an assignment expression?

else if isn’t a statement. The else there is part of the previous if-statement. The if is another if-statement. And it’s not going to somehow turn into an else without a condition. The condition has to be truthy in order for an if-statements body to execute.


The condition in an if-statement must be an expression.
That means that your assignment was indeed an expression.
An expression is something that you can evaluate to get a value.
An assignment expression’s result is the value that the variable was assigned to.
The values that you assigned variables to were 0 and 1, those were therefore the results.

if (0) ... do something
if (1) ... do something

Ah. Point well taken. I should have worded my example differently. Perhaps this is more accurate?

let r = 0
if(r = 0) {
  console.log("This only prints if the expression evaluates to a truthy value. r is equal to zero, right?")
} else {
  console.log("This only prints if the expression evaluates to a falsy value.") 
}

I had no objections ._.
Oh truthy/falsy. Well, that’s what looking leads to discovering. That’s the real point isn’t it. Looking. Making measurements.

1 Like