0

Why is it that when I add curly braces to the filter argument, it does not work?

// Write your code here:

function justCoolStuff(arr1,arr2){

  var newArray = arr1.filter(word => {arr2.includes(word)});
  return newArray

}






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))

Yet, when I remove the curly braces, the code will run. I thought that it is a function? That should be the right syntax for it or am I mistaken?

// Write your code here:

function justCoolStuff(arr1,arr2){

  var newArray = arr1.filter((word) => arr2.includes(word));
  return newArray

}






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))
dallenlee
  • 55
  • 3
  • Please add a proper title to your question. It should explain the issue you have. – B001ᛦ Jun 28 '21 at 13:30
  • You need to use `return` keyword in order to return a value. If you have arrow function without curly brackets, value which follows the arrow is returned by default. If curly brackets are there, you need to use explicit `return`, like so: `() => { return value; }` – Yuriy Yakym Jun 28 '21 at 13:32
  • it's all [documented here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body) – Jaromanda X Jun 28 '21 at 13:32
  • Out of curiosity: is there any reason to use curly braces there? What makes you think that you can randomly use such language features here and there? – Nico Haase Jun 28 '21 at 13:39
  • `(word) => arr2.includes(word)` is a shortcut for `(word) => { return arr2.includes(word); }` (notice the **`return`**) – 3limin4t0r Jun 28 '21 at 13:43

5 Answers5

0

You need return statement { return arr2.includes(word)}

Alex Shani
  • 141
  • 5
0

When adding curly brackets, the implicit return no longer works. You will need to return manually, i.e.:

var newArray = arr1.filter(word => { return arr2.includes(word) });

Refer to the basic syntax of arrow functions.

Terry
  • 63,248
  • 15
  • 96
  • 118
0

It might help you to understand if you format this differently - var newArray = arr1.filter(word => {arr2.includes(word)});

var newArray = arr1.filter(word => {
  arr2.includes(word)
});

If you look at it like that, you're missing your return statement. With parenthesis, the return statement is implied.

Eman4real
  • 538
  • 5
  • 12
0

The use of curly braces after the arrow means the chunk of function code that is to be executed. Therefore, in the first example, you're not returning anything, as you should explicitly say so by using the return keyword. On the second example however, when using that syntax, it is understood that you're returning that boolean, which is what the filter method is expecting.

bernatj
  • 97
  • 5
0

Because those get treated as a start of a function and then you implicitly need to return.

But here is some insights on how you can use braces without returning.

// Write your code here:

function justCoolStuff(arr1,arr2) {
    var newArray = arr1.filter(word => {
        return arr2.includes(word);
    });
    return newArray;
}

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))

Note:

function doSomehting() {
  return 1;
}

// is equal to
const doSomething = () => 1;

// also equal to
const doSomething = () => {
  return 1;
}

// but not equal to
const doSomething = () => {
  1
}
Shravan Dhar
  • 1,455
  • 10
  • 18