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