More on Control Flow in JS


#1

Seems like this should work.

Oops, try again. It looks like your function doesn't return false when hungry is false.
Here's my code:

var hungry = true;
var foodHere = true;

var eat = function()
{  
    if (hungry && foodHere)
    {
        console.log("Great you are hungry and there is food here.");
    }
        
    else
        {
            console.log("You don't get food because you are not hungry or there is no food");
        }

};

#2

I believe the instructions ask for true or false in the return value, not arbitrary text. Sometimes the expectation is very strict and limited.


#3

Thanks for your suggestion. I will resubmit.


#4

Tried to set console.log to true (first statement) and false (second statement).

var hungry = true;
var foodHere = true;

var eat = function()
{  
    if (hungry && foodHere)
    {
        console.log("true");
    }
        
    else
        {
            console.log("false");
        }

};

#5

Those are strings. Try returning the primitive Booleans. true or false without quotes.


#6

I changed the console.log to true (not a string) and false (not a string). I think that's what you are suggesting. Still get an error. Oops, try again. It looks like your function doesn't return false when hungry is false.

`var hungry = true;
var foodHere = true;

var eat = function()
{
if (hungry && foodHere)
{
console.log(true);
}

else
    {
        console.log(false);
    }

};`


#7

This indicates that the SCT is expecting a return value. Currently it is seeing undefined. Change the log out to a return statement.


#8

What is SCT?
I've put a return at the end. Still have an error.
`var hungry = true;
var foodHere = true;

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

else
{
    false;
}
return;

};`


#9

Submission Correctness Test

The return value at the end will still be undefined. Write the statements in each branch of your if statement.

    return true;

    // and

    return false;