Adrian Skar 6300a5339f fix(guide): ES6 markup and edit fixes and improvements (#34691)
- Remove image of phantom
- Remove notes for contributors
- Links to bottom and to markdown + some more
- Alternative code solution

### Notes:
- The guide article mentions the challenge can be solved by using `reduce()` but I couldn't find a way to do so. It might be better to remove that reference and the bottom link until someone provides one.
- The alternative code solution I provided uses a simpler way (in my opinion) to check if the values are integers but does not differ much from the original one. If necessary, feel free to remove it and go on with just the fixes.
2018-12-27 18:09:51 +05:30

3.4 KiB

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

Code explanation:

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

Alternative code solution:


  // change code below this line
  const squaredIntegers = arr.filter((param) => Number.isInteger(param) && param >= 0).map(x => x * x);
  // change code above this line

Code explanation

This does basically the same but uses the isInteger() method to check the numbers.

Resources