Ch. 12 Or / Should This Not Return True Instead of False?


#1

So if the || operator evaluates to true if one or both of the expression are true then should this code not return true instead of false? The expressions tired is set to true. The if function returns true if (tired || bored) are evaluated as true, which according to this lesson, they should be. I instead receive false. Why is this?


tired = true
bored = false

var nap = function() {
  if (tired || bored) {
      return true
  } else {
      return false
  }
};


#2

Because one operand is true, the expression is true, so the return will be true.


#3

I'm not sure you answered my question. I receive a return of false. Through my logic, this tells me that (tired || bored) is false since the code is executing else and returning false. Yet, based on the lesson's information (tired || bored) should be true, and therefore execute the first action in my if statement, i.e. return true.


#4


#5

Actually, the code is not running because it hasn't been called. You're see the console logging of the last assignment, bored = false.

Be sure to call the function,

nap()

#6

Can you give me an example of how calling the function in this particular instance would be written in code?


#7

The above line would be written after the function definition.

var nap = function () {

};

nap();

#8

Okay, that works. Thank you! This lesson is misleading to the student. The code does not execute correctly unless the function is called, yet the lesson grants a 'Success' even when the function is not called. I'm not sure if you are in a position to correct this, but I thought I would let you know anyhow. Thanks again.


#9

We're none of us able to fix the old courses as their editing suite is gone. The courses are essentially archived and will remain in their current form.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.