Though we use higher order arrow functions like in the code below, the solution for this is inefficient, isn’t it?
Reason: Filtering all the positive integers first and then squaring each of them later would double the time.
We could just do both functions’ activity in a single step by checking if it’s a positive integer and squaring it if the condition is true , right?
Yes, but this is a good spot: every array method you use will go through a whole array, so:
// first run through array, filtering:
[4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2].filter(isPositiveIntegerFunction).map(squareFunction);
// second run through [filtered] array, mapping:
[4, 42, 6].map(squareFunction)
// end result:
[16, 1764, 36]
JavaScript is extremely quick, so this doesn’t tend to be a huge issue in practice. You can use reduce to do it in one step, but the code will be more difficult to read.
basically, in the reduce, the accumulator starts as an empty array. For each number, we check if it’s positive and, if it is, we push it’s squared value onto that accumulator. One step, filter and map.
There is argument back and forth whether this is the better way. In theory, it’s a single pass through the array, so there’s that, but it is a little less readable than the filter/map version. It’s largely a matter of personal preference.