11 - "And?"


#1

I get this error message: "Oops, try again. It looks like your function doesn't return false when hungry is false."
What's wrong with my code? Thanks!

My code:

var hungry = true;
var foodHere = true;

var eat = function() {

if (hungry && foodHere) {

  console.log("Let's eat then!");

}

else {

  console.log("Forget it then.");

}

};

eat();


#2

you will want a return instead of a console.log within your if/else function... for example

var hungry = true; 
var foodHere = true; 

var eat = function() {
    if(hungry && foodHere === true) {
        return true; 
    }
    else {
        return false; 
    }
};

hope this helps you


#3

Hi and thanks for the response.
Why do I want to use return rather than console.log when I want my code to print out a response?
Thanks!


#4

By the way, aren't hungry and foodHere already true, so wouldn't it be redundant to write (hungry && foodHere === true)? Thanks!


#5

you are definitely right about the (hungry && foodHere === true) being redundant and within the directions it says to use a return code for this particular function. return values are meant to return the value that comes out of the function.
(this website was helpful for determining between console.log and return)


#6

Ah yes. I see that now. Still need to get a better grasp of the lingo.
Thanks a lot for the help!


#7

I had the same problem.. Now it is working. Thank you! :smile:


#8

with "return" instead console.log()


#9

Thanks for the reply about return, but still no explanation why redundant
code is the way to go. I had basically the same code and keep getting all sorts of weird errors. Please answer the question about the redundancy!


#10

Thank You for posting this code. I finally had to copy and paste it just so I could move on to the next lesson. There should have been a 'hint' provided because frankly this code makes NO sense when comparing to instructions. I mean, AGAIN, if these variable are initially set to true, why does the code have to later be written ===. What's the point of one if you are just going to have to do the other!


#11
hungry && foodHere
and 
hungry && foodHere === true

have the same output so you can use the one or the other (usually you use the shorter one if you know how it works). The only difference comes up when hungry and/or foodHere could be other things then boolean then the === true might be necessary otherwise you can test:

foodHere is true -> foodHere === true --> true (initial value of foodHere)
foodHere is false -> foodHere === true --> false (initial value of foodHere)

But the error was not about this line but about using console.log instead of return, wasn't it?