Code Challenges: Intermediate JS - IsTheDinnerVegan

Hey !

I am running through the code challenges for JS and am currently on Intermediate JS- isTheDinnerVegan.

Prompt : Write a function isTheDinnerVegan() that takes in an array of food object and returns a boolean value based on whether or not every item in the array has entirely plant -based origins.

Issue: I believe I accomplished the task using two variants of function syntax because I get the correct output, but Codecademy prompt says it is still in correct. Any Help in trying to understand if I am missing something in my code that Codecademy sees?

code:

// Write your code here:

// .every() filters out based off a condition and returns a boolean value

/*

const isTheDinnerVegan = (arr => {

return arr.every(value =>arr.value === ‘plant’);

}); // Works, but codeCademy says it doesnt.

*/

function isTheDinnerVegan(food) {

return (food.source === ‘plant’) ? true : false;

}

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))

// Should print false

Hello,

It may seem like it works, but you should try it on an array of ingredients that should return true. Neither one will currently be able to return anything other than false

I suggest creating another array of objects that should return true so you can test with both of them.

Only read further if you want more insight into your existing code. You may go another direction once you test with both, so it may not be needed.


Your first one is the closest right now because it iterates over the array of ingredients. However, arr.value isn’t what you need to compare to the string plant. arr is the array itself, but your arrow function parameter is named value. So value becomes the object that is being checked. You would use value.property with property what should be equal to plant, which is source.

Summary

That’s why arr.value should be value.source


Your second one is only checking .source of the array itself, which will be undefined. It isn’t iterating over the array to compare each object’s source property to the string plant.

Thank you! Not only did you provide great insight, but your explanation was shared in a way that was easily understandable for me! I am a self taught programmer using Codecademy and have a 19-week boot camp under my belt, so I am still learning alot.

So If I am understanding correctly, could i accomplish the same thing if I left my code as is, but changed the arrow function to:

const isTheDinnerVegan = (arr => {

return arr.every(arr => arr.value === ‘plant’);

});

My understanding of my code is that if i pass arr as a param in my arrow function, the value of the array should be checked (arr.value )which should represent food.source? Just want to make sure I am understanding your advice correctly.

thank you again!

You’re welcome. It’s good you’re asking questions and working through these challenges! It’s a great way to continue building your skills and experience.

Not quite, I think there is still a little confusion about what I was saying needed to be changed. The issue wasn’t your parameter name of the arrow function. You changing the parameter name to the same name as the array could cause confusion, even though JavaScript will allow it. The deeper issue is the property you’re using for the comparison.

This was your original code:

const isTheDinnerVegan = (arr) => {
  return arr.every((value) => arr.value === 'plant');
};
  • arr is the array that gets passed in, which is your dinner array.
  • You’re using the .every() method on the array so that you can iterate through every entry in the array.
  • You pass an arrow function to .every() with a parameter of value.
  • This arrow function gets called for each entry in the array, and that entry is sent as an argument to the arrow function.

Since the parameter was named value, this means that you have access to the individual object of the ingredient for each call on the value object. For example, within your arrow function, value could be this:

{name: 'hamburger', source: 'meat'}

or

{name: 'ketchup', source:'plant'}

depending on how far into the array .every() currently is.

So assuming that value is one of the objects above, how would you access the property containing meat or plant?

You would use value.source since that’s value is the name of the parameter, and source is the name of the property you want to check.

If you had changed the parameter name to arr even though it might be confusing, you’d still need to use arr.source for it to match the source property of the object.

I hope this helps. I know it’s long and covers a lot of things you probably already know, but I wanted to reiterate some to clear up any confusion and to help people that may stumble upon this thread in the future who are going through similar trouble.

This was so helpful! Thank you so much!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.