FAQ: Code Challenges: Intermediate JavaScript - isTheDinnerVegan()

This community-built FAQ covers the “isTheDinnerVegan()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise isTheDinnerVegan()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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

const isTheDinnerVegan = arr => {
if(arr.source === ‘plant’){
return true;
}
return false;
};

console.log(dinner.every(isTheDinnerVegan))
// Should print false

Can someone explain why this did n’t work. I believe I am getting the correct out put

3 Likes

Hello,

I wonder the same as not all the array items are plant based so it should return false but I get an error to say that it should return true so I tested with the array in example which is all plant based and it returned true. I still get an error though… confused.com!

A

1 Like

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

is the answer, but I put {} in the code like below and it returns ‘undefined’.

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

The part is a single-line code and I know I don’t have to put {} around it, but I don’t understand why it returns a totally different result.
I think JS is quite strict for these kinds of things, and it would be very helpful if you make a lesson for it.

6 Likes

If you use the optional { }'s in an arrow function then you also have to use return to return the result.

With the arrow function you can either have a ‘block body’ or ‘concise body’:
Block Body:

const sayHello = name => { //have to use return with block body
  return `Hello, ${name}!`;
}
//could all be on one line, but still need return:
const sayHello = name => {return `Hello, ${name}!`;}

console.log(sayHello('Larry')); //Hello, Larry!

Concise Body:

const sayHello = name => `Hello, ${name}!`;

console.log(sayHello('Larry')); //Hello, Larry!

4 Likes

I really wish someone would give an answer to this. I used the exact same code, got correct results, and was marked wrong as well. I don’t understand what I’m doing wrong but this has happened with several of the code challenges so far. My best guess is that we’re not solving the problem exactly like the answer by using the .every() function. I did look at the alternate solution though and it was highly confusing and seemed overthought, but I could just be not understanding things.

Here’s the alternate solution they give:

Blockquote
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
}

1 Like

Hello @kgnunn999.

I can’t say why your code may have failed since I haven’t seen it, but the reason @netace86605’s code failed the SCT (submitted code tester) even though it returns the correct output has to do with the way the SCT was written. I ran similar code just now, and then opened the test.js file to find this:

        const newDin = [{name: 'one', source: 'plant'}, {name: 'two', source: 'plant'}, {name: 'three', source: 'plant'},]
        expect(isTheDinnerVegan(newDin), "Your function should return `true` if every object in the array has `source: 'dairy'`.").to.equal(true)

The pertinent part is the function call: isTheDinnerVegan(newDin). The function is called with an array of objects passed in as the argument. The way @netace86605’s function works is by passing one object at a time to the function rather than the entire array of objects. Unfortunately even though the solution @netace86605 came up with works perfectly, the SCT isn’t written to test it properly.

The instructions for the challenge do state:
image

So technically the instructions were not followed since @netace86605’s function only expects a single object at a time rather than an array of objects.

Hopefully this helps. Happy coding!

1 Like

source is the property of an object. You have to access the property of object. so it would should be
arr[i].source

please try for loop.

1 Like

It works fine with for loop! But the challenge is to make it work with .every() method.

With .every():
const isTheDinnerVegan = array => array.every(ingredient => ingredient.source === "plant")

2 Likes

Thanks for this! I get most of why this solution works, but don’t quite understand what the arrow is doing inside this callback function (if it even is a callback function… I’m still struggling with things conceptually):

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

Can someone please translate the meaning of this? It seems like there would need to be some if/then logic happening within this method, and that we’re perhaps defining it here. The documentation indicates as much but I’m still struggling to put the concept into words. Would this be saying something like “if for every ingredient, the ingredient source is ‘plant’”?

2 Likes

This is what I did for this excercise, and I’m getting the correct output, but it won’t let me pass…:

const isTheDinnerVegan = (dinner) => {
if (dinner.every === ‘plant’) {
return true;
} else {
return false
}
}

Now to as to understand why, it’s beyond my grasp as of yet.

The hint tells you to consider using .every(), but there is no requirement to do so.

So in this exercise we’re provided the suggestion to use .every() method, and a link to the MDN documentation which indicates syntax as follows:

arr.every(callback(element[, index[, array]])[, thisArg])

In this code which is offered by Codecademyas the alternative solution using the .every() method:

isTheDinnerVegan = arr => arr.every(food => food.source === 'plant'); 

Can someone please answer these questions?

  1. Is food the element as referenced in the MDN documentation?
  2. Also what/where is the callback in this code?

Can someone please explain to me why I am getting ‘undefined’ as my result when I run my code?

function isTheDinnerVegan(arr) {

   arr.every(function (food) {

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

     return true;

    } else {

     return false;

    }
    });

};

Hey Daniel, I am struggling through these myself but I saw you hadn’t had a reply so I thought I’d try running your code. I don’t exactly have an answer but I made yours work by switching to an arrow expression. Here are my solution (partially cribbed, I must confess, from this forum) and yours working.

const isTheDinnerVegan = dinner => dinner.every(food => food.source === 'plant');

const isTheDinnerVegan2 = dinner => dinner.every(food => {

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

     return true;

    } else {

     return false;

    }
    });
1 Like

Yeah. I was able to get it to work as an arrow function as well. I just wanted to try to get it to work the other way, and it is really frustrating that it doesnt.

I spent so much time on it. I was just talking to someone today that one of my shortcomings in learning to code is that I get obsessed over minutiae like that - it worked fine as an arrow function, but I stil spent 1.5 hours trying to get it to work the other way. I spent 1.5 hours to fix code that I was able to get to work one way but not another. Ugh.

If anyone sees anything, please let me know.

I don’t know. To be honest I’ve got in the habit of just using arrow functions. Keep telling myself I should practice the other format. But to encourage you I don’t think spending time on one thing is such a bad idea. I got totally stuck on the last project but when it was finally finished I found I was so much more fluent afterwards because I’d spent all that time staring and unpacking in detail.

1 Like

Hello everyone. Hope that somebody can explain this to me.
Working on the solution for isTheDinnerVegan(). This is my code. Getting right output, but system tells me it is wrong. Please explain, why? Thank you.

Hello
Can somebody please explain what is wrong with my code?
const isTheDinnerVegan = arr => arr.every(el => {

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

  return true;

} else {

  return false;

}

}