FAQ: Code Challenges: Intermediate JavaScript - isTheDinnerVegan()

In the above code, you are using i correctly (as in you are using it to index into array). Rather than point you to an article, I think it would be more beneficial to review the lessons on for loops, arrays, and array indices.

All functions must return something. When there is no explicit return statement in the function, the default value of undefined is returned.


I think what could be very useful is to take a step back and reevaluate what you are trying to accomplish. Let’s look at the big picture. What is the one overall thing you are trying to accomplish with this entire program? Now, take that goal and divide it into smaller, more manageable goals that you can accomplish. Now divide those goals again until you’re left with the simplest goals you need to achieve in order to complete this exercise. What steps do you need to take to achieve those simple goals?

Based on your above code, two questions I think could help guide you toward getting this to work are: 1) How can you figure out whether a certain meal is vegan or not? and 2) Based on the result from question 1, what do you want to do if a certain meal is not vegan?


It’s great that you found an alternate solution using .every(). I encourage you to solve it using this more naive approach; I believe you’ve just overthought the problem, so try answering the questions I put above and see if that helps. Even if it takes several attempts and posts on the forums, keep working on it until you solve it!

1 Like

Finally solved it! Here is the updated code for the loop part:

function isTheDinnerVegan(array){
 

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

} 

So basically, if not every element of the array.source is ‘plant’, it will return false!

Looks great! Happy coding! :slight_smile:

I don’t understand this solution. Where does “food.source” come from in this function? Shouldn’t it be “dinner.source” since “dinner” the name of the variable and we are then referring to the “source” property?

const isTheDinnerVegan const isTheDinnerVegan = arr => arr.every(food => food.source === ‘plant’);

const dinner = [{name: ‘hamburger’, source: ‘meat’}, {name: ‘cheese’, source: ‘dairy’}, {name: ‘ketchup’, source:‘plant’}, {name: ‘bun’, source: ‘plant’}, {name: ‘dessert twinkies’, source:‘unknown’}];

console.log(isTheDinnerVegan(dinner))