justCoolStuff() - Where did I go wrong?


I am currently completing the Intermediate JavaScript code challenges and ran into an issue which I don’t fully understand because my answer appears to be the same as the answer from the solution with a few tweaks in the code.

const justCoolStuff = (arr1, arr2) => arr1.filter(stuff => arr2.includes(stuff))

const justCoolStuff = (arr1, arr2) => {
  arr1.filter(stuff => {

I spent a couple of hours trying to do this question thinking I didn’t have a good grasp on how to use these methods. After this I feel I do have a good grasp, I am just missing something to develop my understanding a bit better.

Why does my answer with the curly brackets return undefined when it is still the right way of writing a function, just not the concise way.

Any help is appreciated!

Hi, Moiza.
I’m going to attempt to answer your question. I think the reason is because the Codecademy answer uses the simple expression of an arrow function declared on one line and so does not need the “return” keyword because it is implicit.
Yours is a multiline statement and so you should explicitly use “return” inside your function: return arr1.filter(…), without which it returns “undefined”. Cheers

The following below is a screenshot from MDN Web Docs on Arrow Function Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Hey Chu,

Thanks for your response, I tried adding the return before the arr1.filter(…) but all I get is an empty array. Still scratching my head on this one.

I appreciate the help though!

I was able to refactor the code. I got rid of the extra curly braces in the version that took more than a line.
I don’t know Javascript well so I don’t know about the filter function, but without !.return statement, the output was “undefined”.

Oh I see! Wow, so many little nuances, thank you so much!