12/14 not sure what's going on here


#1

var tired = true;
var bored = false;

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


#2

Your if-else
should be an else if
https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/if...else


#3

Don't do an else if, just do an else.

It should look like this:

var tired = true;
var bored = false;

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


#4

An else dos NOT have a condition !!!!
follow the developer-mozilla link mentioned above....


#5

Actually, the instructions say that the first if statement returns true if tired or bored equals true. Therefore, there should only be an "or" operator used on the first if statement. Here is what I got:

var tired = true;
var bored = false;

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


#6

The Instructions say "statement that returns true if either tired or bored (or both!) "

I made my first part of the if statement like this:
if (tired || bored || tired && bored) {
Which basically means this:
tired or bored OR tired && bored

Then I used an else to return false.


#7

none work ok???????????????????????????????????????????????????????


#8

it's this
var tired = true;
var bored = false;

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

nap();


#9
either tired or bored (or both!)

That's what or already does:

A || B is true if either A or B or both are true. The reason this is emphazised is probably that in common language or often is used as XOR which only includes A or B but not both. So here tired || bored || tired && bored the last part is redundant as or already does the job.


#10

Thanks man it was the closing.


#11

Thank you all for your replies issue solved


#12

I did exactly the same except for this following part:

var nap = function(tired, bored) {

I put tired and bored as parameter in the function, which didn't work correctly, wondering why?


Same confusion on 13/14 and 12/14...Big "?"
#13

This is probably the problem. Don't get me wrong it's probably the better idea, but it also creates 2 new variables. So you have tired and bored outside the function and you have tired and bored inside the function which are not connected by anything but the function call. So if a test function does not expect it to have parameters and just calls nap();. Your tired and bored parameters will be set to or better stay at undefined. And if someone tinkers on the global variables to see a different function output it will also fails as the parameters are not connected, as said.
Hope this makes the problem a little clearer.


Same confusion on 13/14 and 12/14...Big "?"
#14

So even it's the same name inside and outside, it represents different variable? Thank you for your patient answer.


#15

The thing is that function have their own scope and on each scope you can declare variables that only exist on this particular scope. Now as parameters by default are local variables (same as any other var name) by using them you create a new variable in this scope. What happens when you use an existing name is so called "shadowing" which is shown in this exercise:
https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661
The global variable is now no longer accessible from the local scope because the local variable shadows it by occupying the name.