isTheDinnerVegan - Two-line solution

I’ve spent quite a bit of time on MDN during the past couple of days. I wanted to get a good grip on arrays, objects, etc. It’s very important to understand what’s meant by Javascript functions being first-class objects. Also, I can’t understate the power of iterators, as well as the importance of understanding how to use higher-order and callback functions.

Just about every challenge in the Intermediate Javascript section of the course wants to test your understanding of the aforementioned topics. For example, here’s the two-line solution to the isDinnerVegan() challenge:

// Write your code here:

const isPlantBased = ({name, source}) => source === ‘plant’;

const isTheDinnerVegan = meal => meal.every(isPlantBased);

// That’s it!

// Feel free to comment out the code below to test your function

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


// Should print false

const meal = [{name: ‘arugula’, source: ‘plant’}, {name: ‘tomatoes’, source: ‘plant’}, {name: ‘lemon’, source:‘plant’}, {name: ‘olive oil’, source: ‘plant’}];

console.log(isTheDinnerVegan(meal)); // Should return true

1 Like

Hey, Ruby, it’s a clever solution indeed, I remember this challenge gave me a headache. I totally agree w you about getting a good grip on basic concepts, but also (if my memory serves) we only learned every() after a couple more lessons, so it’s nice to look back with fresh (more experienced eyes) and see how much we’ve progressed :smiley: