It's time to look at higher-order functions and their common pair, arrow functions.
Arrow functions work really well when combined with higher-order functions, such as <code>map()</code>, <code>filter()</code>, and <code>reduce()</code>. <br>
But what are these functions? Lets look at the simplest example <code>forEach()</code>, and run it on the following array of sample Facebook posts.
Of the two <code>forEach()</code> versions below, both perform the exact same log function, and each takes an anonymous callback with a parameter <code>post</code>. The difference is the syntax. One uses an arrow function and the other does not.
<blockquote>
<strong>ES5</strong><br>
FBpost.forEach(function(post) {<br>
console.log(post) // log each post here<br>
});<br>
<strong>ES6</strong><br>
FBpost.forEach((post) => {<br>
console.log(post) // log each post here<br>
});<br>
</blockquote>
<code>filter()</code> is very similar. Below it will iterate over the <code>FBPosts</code> array, perform the logic to filter out the items that do not meet the requirements, and return a new array, <code>results</code>.
Use arrow function syntax to compute the square of <em>only</em> the positive integers (decimal numbers are not integers) in the array <code>realNumberArray</code> and store the new array in the variable <code>squaredIntegers</code>.
testString: getUserInput => assert(getUserInput('index').match(/const\s+squaredIntegers/g), '<code>squaredIntegers</code> should be a constant variable (by using <code>const</code>).');
testString: getUserInput => assert(!getUserInput('index').match(/(for)|(while)/g), 'loop should not be used');
- text: <code>map</code>, <code>filter</code>, or <code>reduce</code> should be used
testString: getUserInput => assert(getUserInput('index').match(/map|filter|reduce/g), '<code>map</code>, <code>filter</code>, or <code>reduce</code> should be used');