What is wrong with this code


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/javascript-beginner-en-qDwp0/2/3?curriculum_id=506324b3a7dffd00020bf661

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
According to the compiler my code doesn’t pass false if both option are false/



// Declare your variables here!
var tired = true;
var bored = false;

var nap = function() {
  // Add your if/else statement here!
  if(tired || bored) {
      console.log("tired or bored");
  }
  else {
      console.log("not tired or bored");
  }
};


#2

Hi @blogace67116,

Have you read this part of the instructions?:

Inside the nap function, create an if statement that returns true if either tired or bored (or both!) are true, and false otherwise.

We need to return a boolean, and as I can see, you haven’t done that yet. :slight_smile:


#3

I’m having a hard time understanding this, isn’t the default value of an if statement set to true?

if tired is set to true and bored is set to false, shouldn’t the or in the if statement pass that to true?


#4

Yes, because our variable tired is set to true and our variable bored it set to false; when we compare both in our if statement:

we check if we’re either tired or bored the logical operator “or” (||) which will result in it being true because true || false is true. Our problem here is that we didn’t use our return statement; so if we called our function, we would get undefined because we didn’t make our function (nap()) return anything.

Study your code/review it, and you’ll see that there’s no return statement in there.


I’m confused, do you mean a random if statement? Or yours above in your code only?

If whatever you write in the condition is true, then the statement will execute.

e.g:

if (2 > 1) {
//stuff happens here.
} else {
//otherwise, this will execute.
}

2 is greater than 1; that is true, so the if statement will execute.


e.g #2:

var player = false;

if (player === false){
  console.log("I'm false!"); //#1
} else {
  console.log("I'm not false!"); //#2
}

So, in this example, we check if our variable player is false. Our if statement will execute because this is true: player is equal (===) to false. Otherwise, our else statement will execute.

Does this make sense?


#5

I think I get it, I missed the return part in the instruction and was looking at someone else forum that used console.log.
I’m still not certain of when to use return over console.log, do we use return to add a value to the function?

var tired = true;
var bored = false;

var nap = function() {
  // Add your if/else statement here!
  if(tired || bored) {
      return(true);
  }
  else {
      return(false);
  }
};
console.log(nap);

My console.log displays function: nap, shouldn’t the terminal display true?

[https://www.codecademy.com/en/courses/javascript-beginner-en-qDwp0/2/3?curriculum_id=506324b3a7dffd00020bf661]


#6

Well lets look here:

As you can see, printing it this way will only display the information about the function
e.g (on the console).

[Function: nap]

To call your function properly, we need to add parentheses at the end like so:

console.log(nap());

This way JS recognizes that you’re calling a function (so it can do whatever you programmed the function to do).


Also (if you want to tidy up your code):

You don’t really need to enclose what you’re returning in parentheses; it’s not needed. :slight_smile:

I hope all of this helps! :slight_smile:


#7

Thank’s intermediategamer, this was a tone of help. You cleared a lot of things up for me.


#8

You’re welcome! :smile:


#9

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