FAQ: Code Challenges: Intermediate JavaScript - isTheDinnerVegan()

Is it generally sufficient to use == for comparison when compared to ===?

happened to me as well in the intermediat tests several times… Because they didn’t have a lesson of this concise body of arrow function. And sure you can do it with another code but when they give this concise body solution in the code without further explanation… I thought that this is some kinda thing what I haven’t learned and couldnt even find anywhere. So just went trough and leave them…

Thank you! I was having an issue with my solution because I didn’t switch mine into an arrow expression, after following your advice, it started working :slight_smile:

I can see the date of the question, but maybe my reply will be useful for future students.
So, to make .every() method work you should put the callback function inside. You can use the reference to the previously declared function, or declare a new one.

ingredient => ingredient.source === “plant”

As you can see by the arrow, we declared a new function. “Ingredient” is the reference for each array element, passed by .every() method, which works similar to the loop. Inside the function, we compare if “ingredient” (each element of the array) is equal to “plant”.
Equation always evaluates to “true” or “false”. This result returns in .every() without the word “return” because in our arrow function we don’t have curly brackets. And when .every() gets “true” or “false”, it does its own stuff, which is to return true if all the result of callback function were “true”.

Thank you. I was having the same issue. I spent a lot of time at this problem. But I dont understand why arrow function is the only one solution.

This is a great solution - but I have 2 questions.

  1. Since we are using an if statement:

if (array[i].source === ‘plant’){
return true
}

Why are not using an else statement to return ‘false’? Isn’t that the whole point of an else statement, to give an alternative result for anything that doesn’t meet 'if’s conditions?

  1. I tried two different arrays, exactly the same format, just changed around the food names and the variable names and I keep getting the wrong result (which worked on the initial array and it’s objects). What!!!

Question 1

The code will return false if the if condition is not satisfied. Take a look at the entire block of code.

for (let i = 0; i < array.length; i++){
  if (array[i].source=== 'plant') {
    return true;
  } return false;
}

If if condition is satisfied, true will be returned. When a return statement is executed, the function is immediately exited. Therefore, none of the other code inside isTheDinnerVegan will be executed after return true is run. If the if condition is not satisfied, the next line of code will be run. This is return false. Therefore, simply putting return false after an if statement that returns true is comparable to an if-else statement that returns true if it is true and false otherwise.

Question 2

This is because the code that the user has written is incorrect. As I explained earlier, once a return statement is executed, the rest of the code in the function will not run because you exit out of the function. Therefore, because either return true or return false will be executed on the first iteration of the for loop, you’re actually only checking whether the first dish in the array is vegan or not. What changes would you have to make to the code to fix this?

1 Like

Ahh, I see, so ‘return’ in an ‘if’ statement stops it looping after the first element.

I’m still at a huge loss how to make the if statement return a boolean while not using a ‘return’ command.

Here’s the code I tried to run, which is throwing up an error. Any more suggestions please?

const plantSource = (food) => {

if (food.source === ‘plant’){

 return true;

   }

     return false;

       }

function isTheDinnerVegan (array) {
for (let i = 0; i < array.length; i++) {
plantSource()
}
}

The error is below, but it’s baffling as it’s the same function written in the Codecademy example.

/home/ccuser/workspace/intermediate-javascript-coding-challenge-is-the-dinner-vegan/main.js:2
if (food.source === ‘plant’){
^

TypeError: Cannot read property ‘source’ of undefined
at plantSource

The plantSource function takes in one argument, food. You need to pass something into this function when you call it (e.g. plantSource(someArray[someIndex])). The element at someIndex of someArray should be an object that has source as an instance variable.

In this block of code I added ‘array’ into the brackets when calling the plantSource function:

function isTheDinnerVegan (array) {
for (let i = 0; i < array.length; i++) {
plantSource(array)
}
}

But I’m still getting undefined… please help me out! I can’t find the answer anywhere

A couple of things could be happening since I can’t see your full code.

  1. Make sure you’re passing in an array into the isTheDinnerVegan function; you need to pass an array in for this to work.
  2. Inside that array, you should have objects. Those objects should have source as instance variables.
  3. You want to call plantSource on each of those objects. Hint: make use of the i variable you have in your for loop declaration.
1 Like

// Alternate solution:
// Using a function declaration, loop, and helper function:
function isTheDinnerVegan(arr) {
const isVegan = (food) => {
if (food.source === ‘plant’) {
return true;
}
return false;
}
for(let i = 0; i<arr.length; i++){
if (!isVegan(arr[i])){
return false
}
}
return true
}

Hello everyone, this is the alternate solution given by codecademy. Can anyone explain to me why is there an additional ‘return true’ statement outside the for loop? Thanks!

Welcome to the forums! You can format code using the </> button for better readability and to preserve indentation.

Once a return statement is executed, you immediately exit the function. This means that no more code inside the function will run for that particular function call. Therefore, if one of the food items had a source that was not 'plant', return false would execute and the function would be exited. However, if all of the food items had 'plant' as their sources, then we’d complete all the iterations of the for loop without returning anything. In this case, since we know that all of the food sources are 'plant' we can return true.

Example

function isEven(array) {
    for (let i = 0; i < array.length; i++) {
        if (array[i] % 2 != 0) {
            return false;
        }
    }
    return true;
}

In the above example, we made a function where we want to return true if all the numbers in an array are even and false otherwise. If at any point we encounter an odd number, return false will execute and the function will be exited. However, if all the numbers are even, we will finish going through the for loop without returning anything. Because we haven’t returned anything and the function is still executing, we can return true because we know that all the numbers in the array are even.

1 Like

Thank you so much for your explanation!! It is very clear! I understand now :grinning:

(And oops thanks for telling me about the </> button XD first time using the forum so I don’t know the functions very well :sweat_smile:)

1 Like

Thanks victoria!

Here’s what worked, legend

function isTheDinnerVegan (array) {
for (let i = 0; i < array.length; i++) {
return plantSource(array[i])
}
}

Edit: For God’s sake… nope didn’t work, because the ‘return’ doesn’t let the loop work on every element, so the result I was getting was only in reference to the first element.

I am at a complete loss here, all I keep getting is undefined.

I also tried

‘plantSource(i)’

in the above code and still getting undefined…

How do I do this without a return statement?!

This won’t work. i is an integer, not an object from which you can determine whether it is vegan.


Please post all of your code (everything) and format it according to the topic below so that we can get a better sense of what is going on.

1 Like

Thanks Vic

Here’s the code in it’s entirety:


  if (food.source === 'plant'){

    return true;

}

      return false;

}


function isTheDinnerVegan (array) {
   for (let i = 0; i < array.length; i++) {
     return plantSource(array[i].source)
}
}

Running the code with this: return plantSource(array[i].source)

gives a true or false result, but wrongly so (as in the answer is not accurate).

Is this all of your code? If so, the plantSource function doesn’t exist. You need to create this function and put your if and return statements inside it.

Also, take a look at a previous reply I made in this topic that explains how return works and why you shouldn’t be using it in your specific case inside the for loop.

I love the conciseness of arrow function

const isTheDinnerVegan = array => array.every(elem => elem.source === 'plant');

Oops, I made a mistake, I left out the actual function.


 const plantSource = (food) => {
if (food.source === 'plant'){
  return true;
}
 return false;
}


function isTheDinnerVegan (array) {
   for (let i = 0; i < array.length; i++) {
   plantSource(array[i].source)
}
}

I really don’t know what to do with this now, there have been a couple of Codecademy problems where i just haven’t been able to solve them, and it leaves a real sense of frustration and incompleteness.

a) In terms of the ‘i’, I’m not sure how else to use it with the plantSource function. Is there any article you recommend which could directly lead me to the solution for this?

b) When I remove the ‘return’, all I get is an undefined result. If I use console.log, I just get a print out of true/false for each element, rather than one true or false result.

Note: I solved this using the .every function, but until I solve it ‘manually’, I don’t think I’ll ever be a decent programmer worth my salt.