Write Higher Order Arrow Functions I'm totally lost

Tell us what’s happening:

This is the first time here when I totally had no clue how to acomplish the task but even more don’t understand the solution after copypasting it from hints. There are so many new things happening at the same time that I’m totally lost. Did it escalate to fast or is it just me?

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);

// wuba luba dub dub!!!!!!!!!!!!!!


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36.

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

It would be helpful, if you would give us a hint, what you do not understand.

Further reading:
filter()
parseInt()
map()

I guess I just have to spend more time with the task and all the methods are needed to acomplish it. My question is more philosophical - “All previous task were relatively easy (at least to understand when you look at the solution) and this one is like a concret wall that you hit at 100 mph”. And I wonder is it only me who feel that way or this task is really considerably harder than usual?

All the challenges are designed to be completed with only the knowledge of previous ones, and any that doesn’t should be considered a bug. That said, the difficulty does eventually ramp up and the training wheels come off, at which point it’s expected that you use outside resources such as google and MDN to find further documentation and examples of what you need to use.

Now to take the other side for a moment: FCC would at some point like to become a fully self-contained learning curriculum, but it’s not quite there yet. Ultimately, it needs the input of some education professionals to find where the real gaps are; the problem with FCC is that it’s written by programmers who already know the material – as they’d naturally have to, but this does leave them blind as to where complete beginners are going to stumble. In this case, some additional lessons on chaining together map and filter methods would probably go a long way.

1 Like
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {

  // compute the square of only the positive integers (decimal numbers are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers.

 const squaredIntegers = arr
  // only positive
  .filter( (num) => num > 0 )
  // only integers
  .filter( (num) => num % parseInt(num) === 0 )
  // compute the square
  .map( (num) => Math.pow(num, 2) );

  return squaredIntegers;
};

const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

I wrote some comments into the code and made the code more readable (2 filter instead of 1)

1 Like

This is the solution that I did.
I gonna try to explain my code but consider the fact I’m not the best in coding lol.

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


const squareList = (arr) => arr.filter( (num) => num > 0 && Number.isInteger(num) ).map( (num) => Math.pow(num,2) );

const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Let’s start …

1) To find first the positive integers ( not decimals) in the realNumberArray we can use filter() function to pick just the desired numbers. This is the syntax of filter()

array .filter( function(currentValue, index, arr), thisValue )

array is arr
function is ( (num) => num > 0 && Number.isInteger(num) )
currentValue is num ( This is the only parameter used in our function)
index, arr and thisValue are optionals

See how we declared our function with the arrow function syntax!!!, not using the keyword ‘function’… Now, let’s explain this function.

  • The first (num) is the actual parameter in the function (our currentValue), remember that if you use arrow function syntax, you can declare the parameters in parenthesis
    ( ) before using the arrow.

  • After the arrow =>, it comes the body portion of the function. We have num > 0 which gives us the positive numbers greater that zero, and ( && ) , Number.isInteger(num) is a boolean function that returns true if it finds integers (not decimals).

2) Now, we have the desired numbers from the array, we can calculate the square of those numbers. This is why I used map( (num) => Math.pow(num,2) ); I think I don’t need to explain this because its syntax is pretty much the same of filter().

3) In const squaredIntegers = squareList(realNumberArray); automatically stores the return values from squareList() to the const squareIntegers .

Hope this helps you! :grinning:

2 Likes

This particular challenge has been an issue for many campers before you. It has recently been rewritten so that you do not have to figure out as much as the current challenge does.

Take a look at the same challenge on our beta site (see link below) which will soon be moving to production.

https://www.freecodecamp.rocks/learn/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions

See if this version is easier to understand and complete.

1 Like