Code challenges: Intermiediate JavaScript - justCoolStuff

Hello,
I don’t understand why I can’t use curly braces in this :

// Write your code here: const justCoolStuff = (arr,arr1) =>{ return arr.filter(word=> { arr1.includes(word)} )} // Feel free to uncomment the code below to test your function const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos']; const myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway']; console.log(justCoolStuff(myStuff, coolStuff)) // Should print [ 'fruit-by-the-foot', 'skateboards', 'my room' ]

Two things needed to be fixed to get your code to run correctly.

const justCoolStuff = (arr,arr1) =>{ return
  arr.filter(word=> {
  arr1.includes(word)}
  )}

The curly braces on the function body are not the problem. It’s the return on its own line. The return value needs to be on the same line as the keyword.

return arr.filter( ...

The second problem relates to curly braces, namely there must be a return keyword when they are used.

    return arr1.includes( ...

This is what it will look similar to once the fixes are done:

const justCoolStuff = (a, b) => { 
  return a.filter(word => {
    return b.includes(word)
  })
}

With regard to how the above might be written when there are no curly braces on either function or method.

const justCoolStuff = (a, b) => a.filter(word => b.includes(word));

This is allowed syntax but not necessarily preferred since both body and return are implicit. The curly brace syntax is easier to read and debug, and shows its structure better. Still, when we are comfortable with the concise form, it is perfectly valid.

1 Like

Thanks, that makes a lot of sense.

1 Like