freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../es6/write-higher-order-arrow-fu.../index.md

4.1 KiB

title
Write Higher Order Arrow Functions

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

Problem Explanation:

We need to compute and square values from the realNumberArray and store them in the variable squaredIntegers using the map(), filter(), and/or reduce() functions.

:speech_balloon: Hint: 1

  • You will need to filter() the realNumberArray for positive integers (decimals are not integers).

try to solve the problem now

:speech_balloon: Hint: 2

  • You will need to map() the values from your filter() function to the variable squaredIntegers.

try to solve the problem now

:speech_balloon: Hint: 3

  • Remember the magic of chaining functions.

try to solve the problem now

Spoiler Alert!

warning sign

Solution ahead!

:beginner: Basic Code Solution:

    const squareList = (arr) => {
      "use strict";
      const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
      return squaredIntegers;
    };

    // test your code
    const squaredIntegers = squareList(realNumberArray);
    console.log(squaredIntegers);

:rocket: Run Code

Code Explanation:

Uses the operator filter() and map() functions to square all positive integers in a given array.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.