Tell us what’s happening:
So, I wanted to be sure I am understanding how these work properly. I’ve been moving in to a software developer career over the past year, and find myself constantly questioning how/why things work, and I’ve come to realize a common issue in all of my “stuck” moments is in a lack of writing out what I’m doing and what I need to do. I plan to start writing blog posts to help combat this and hopefully reinforce new concepts I’ve learned. Here is my written explanation and break down of how this is supposed to work:
Higher order functions are simply functions that accept other functions as an argument.
(Argument being the thing that goes inside the () at the beginning of a function.)
For example, we pass a function to filter() as an argument. This function is called a
callback. When used to filter an array, it would be called for every item in the array,
such as:
const realNumbers = [4, 5.7, -1.2, 3.14, 24, 25, 7];
const squareList = (arr) => {
const squaredNumbers = arr.filter((num) => num > 0 && num % parseInt(num) === 0)
}
In this function, we are using filter() with the arguments of (num) and the function
(num > 0 && num % parseInt(num) === 0) to filter the array and remove decimals. We use
parseInt(num) to parse a string argument and return the number.
So the entire line reads as:
function (num) {
when num > 0
and
num remainder "num" equals 0
do next steps
}
When the input number is greater than 0 and the remainder of the input number
(turned in to a string to remove decimals) is equal to 0, move on to the next steps.
So we move on to the next step of the function:
const squaredNumbers = arr.filter((num) => num > 0 && num % parseInt(num) === 0)
.map((num) => Math.pow(num, 2));
After the filter has parsed through the array and separated the numbers from decimals, it
moves on to map() over the remaining numbers, creating a new array based off the inserted
argument (num). We use Math.pow, which takes the base number(in this case the "num" argument)
and returns its exponent (2), which is simply squaring that new number.
This creates the new array of squared numbers, without the negatives and without the decimals.
Could I get some thought/insight/clarification on this? Am I understanding it correctly?
Thanks in advance!
Your code so far
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
// change code below this line
const squaredIntegers = arr.filter((num) => num > 0 && num % parseInt(num) === 0).map((num) => Math.pow(num, 2));
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions