# Can't understand this ES6 solution. What is 'num'...?

Hallo.

I’m working through the ES6 challenges, and after much frustration, I took a look at the hints…and I still can’t wrap my head around it fully.

Here’s the solution code, which does indeed pass the tests:

``````const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
return squaredIntegers;
``````

My question is this: what is ‘num’ in this equation? It’s not declared as a variable in the rest of the code, and I can’t find any explanation of it online as a JS component. I’m just starting to get my head around the arrow syntax, but I can’t quite follow the above.

1 Like

The ‘num’ is the argument to the lambda function. This part:

`(num) => num > 0 && num % parseInt(num) === 0`

is the lambda function or arrow function. It is a special syntax for an anonymous function, that is, a function that has no name. They are usually used only once, so there’s no need to define a name for them. When you see the `=>` it is an arrow function syntax.

In this case specifically you’re passing this function to filter the numbers in the arr. Only the numbers that are greater than zero and have no decimals will be kept.

You can see more here

Hope it helps.

What @fferreira12 says, just note these are the same:

`````` arr.filter(function (num) {
return num > 0 && num % parseInt(num) === 0
});
``````

Or

``````function isPositiveInteger (num) {
return num > 0 && num % parseInt(num) === 0;
}

arr.filter(isPositiveInteger);
``````
1 Like

Yeah, I get that the arrow syntax is equivalent to an unnamed function in standard syntax, but in every case I’ve seen so far, `function(value) { ...` takes the parameter value from elsewhere in the code. In this instance, I can’t see where num comes from. Is it a variable?

In any case, further reading online seems to suggest that the ES6 challenges appear out of order in the curriculum, and that it’s best to skip them for now and come back when I’ve covered the higher level functions like map, filter, etc…

higher level functions are functions that take an other function as argument. These array methods then take one by one the elements of the array and feed it to the callback and then do something with the returned value.

As such, `num` is one element of the array

1 Like

`filter` takes a function that runs once for each element in the array, `num` is just the argument for that function and will be the current element.

I just clicked why you’re having problems, and I’d strongly advise ignoring the ES6 section until you’ve done the functional (and OO) programming sections. Once you’ve gone through that, it’ll make more sense as it introduces filter/map/etc

1 Like

Yup, I think I understand what num does in this instance…I just can’t see how it’s defined, unless ‘num’ is some kind of selector?

Could you replace num with, for example, ‘val’, for the same result, or is ‘num’ a special term?

Aha!

Thank you! Between @ieahleen 's answer and yours, I think I get it now.

Much appreciated, folks.

2 Likes

Yes you can replace it with anything, it’s literally just a function argument – like

``````function add1(num) { num + 1 }
``````

Could be

``````function add1 (hullabaloo) { hullabaloo + 1}
``````
1 Like

it’s a function parameter, you can call it `bananaSplit` and it works the same
a callback is just a normal function, it is the filter/map/forEach/every/some/… method that takes one element at a time and feed it to the callback function

1 Like