Write Higher Order Arrow Functions (help)

Tell us what’s happening:
I have been working on ES6: Write Higher Order Arrow Functions, I am a little stumped, to say the least.
should I have already come across the following? :

          map() filter() reduce()..

So far I have completed HTML, CSS & JAVASCRIPT BASICS…
but know nothing about these.
Should I just go do some research or is there a module on these through freecodecamp?

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;
  // 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/70.0.3538.110 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions

2 Likes

IIRC this is the first place FCC references those, but you’ll really focus on them in the Functional Programming section. A higher-order function is just a function that takes a function as an argument, or returns a function. Map, filter, and reduce are examples of higher-order functions that are built-in to JS. It can’t hurt to skim MDN’s page on one of those, like filter():

1 Like

it is your chance to train your document searching abilities!

(but there have been many critics on where the ES6 section is placed, so you are not the only one to be really stumped with it - it will make sense tho!)

2 Likes

I am glad I am not the only one who was completely stumped by this section. I am also still not sure why Arrow Functions are a big deal. I have read a few different websites that describe their purpose, but I guess until I have some real world experiences with it, I will remain slightly clueless.

2 Likes

Thank you for the reply :ok_hand:

I will do that now.

They are useful with high order functions (or every time that you need an anonymous function), and let you write more compact code. For example if you need to filter an array to get only even numbers:
let newArray = array.filter(function(e) {return e%2 === 0;});
With arrow function:
let newArray = array.filter(e => {return e%2 === 0;});
Or in this case that the function is just a return statement even more compact:
let newArray = array.filter(e => e%2===0);

2 Likes

I thought maybe I had missed it too!
Well, so it is google and read then hahaha.

I would suggest to include a phrase like “don’t panic if you never have heard of these terms …you will be introduced to them in future lessons”.

2 Likes

I have to correct myself. an introduction in the future makes no sense since THIS task requires one to use these higher order functions.

So i guess I have to acquire the required knowledge from the object oriented programing and the functional programing sections before I return to this task to solve it?

Anyway, although it is not the purpose of this lesson, this is how the same result can be achieved without any of the new stuff :stuck_out_tongue:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];

let arr = [];
for(let i = 0; i < realNumberArray.length; i++){
    if(realNumberArray[i] > 0 && realNumberArray[i]%1 === 0){
      arr.push(realNumberArray[i] * realNumberArray[i]);
  }
}

console.log(arr); // result is 16,1764,36
1 Like

return e%2 === 0;
Why is my brain not working out this return statement? I been staring at code for so long, brain freeze…
%2 === 0 ? please explain lol

e is one element of the array
e%2 - % is the reminder operator, so that will be 1 for odd numbers and 0 for even numbers
So, if e%2 === 0 the number is even, it returns true and the number is included in the filtered array. If it is odd then it returns false and the number is excluded by the filtered array.

1 Like

Thank you for the quick response. I feel like my brain is fried haha… trying to learn javascript as quick as possible.

1 Like

Mozilla has all the necessary info to finish this exercise. I hope these links will help.

.filter
.map
.isInteger

In case you’re blocked…

const squaredIntegers = realNumberArray .filter(num => num > 0 && Number.isInteger(num)) .map(posInt => posInt * posInt);