Code Challenges: Intermediate JavaScript / isTheDinnerVegan()

Hello guys,

On this exercise, I had an “undefined” that was logging with my code. While checking with the solution (isTheDinnerVegan()), I found my error was just to have added some { } to my callback function (isTheDinnerVegan2()) inside the .every() method:

I just don’t get it, can you explain me please ?

Thanks a lot,
Victor

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

const isTheDinnerVegan2 = (arr) => {
  arr.every(item => item.source === "plant");
};

const isTheDinnerVegan3 = (arr) => arr.every(item => item.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)) /* Solution: prints "false" */
console.log(isTheDinnerVegan2(dinner)) /* My 1st attempt: prints "undefined" */
console.log(isTheDinnerVegan3(dinner)) /* My 2nd attempt: prints "false" */

to write a function, we can do:

const functionName = (param1, param2) => {
   return 'hello world'
}

then there are some shorthand’s, like when the body of your function is a single line, we can omit return and {}:

const functionName = (param1, param2) => 'hello world'

now the string is implicit returned. But this shorthand only works when both curly brackets and return are omitted.

if the function only has on parameter, the brackets can also be omited:

const functionName = name => `hello ${name}`

Thanks a lot I got it!

Victor