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!
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.
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!
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
}
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:
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.
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’”?
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.
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.
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.
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.