Write Higher Order Arrow Functions using Math.pow(value, 2) help please

Write Higher Order Arrow Functions using Math.pow(value, 2) help please
0.0 0

#1

I need help passing the final check on this lesson.
I pass all the tests except retuning the squared values. If you look at my code you can see that I need to integrate my commented out line " Math.pow(squaredIntegers, 2); " into the "const squaredIntegers… " line.

I am fuzzy on the syntax here, I have tried adding it to the beginning of the line like this:
const squaredIntegers = Math.pow(arr.filter( (arr) => Number.isInteger(arr) ), 2;
but then sqauredIntegers it says is not an array, so it fails the test.
I have tried adding it at the end so the Math.pow is calculated after, and I have tried a lot of other things too!

Any tips or suggestions greatly appreciated!

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr.filter( (arr) => Number.isInteger(arr) );
  //Math.pow(squaredIntegers, 2);
  // 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 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0.

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


#2

filter fills an array with the element that returns true for the provided callback.
And you are doing that correctly.

However you then want to modify each element in that array to return a different one.
filter won’t be much of a use here… but there are other methods that can help us; for instance map.

You can for example chain the two methods to obtain a newly fresh array:

arr.filter(-> keep only some).map(-> change the filtered one to a new value)

Or you can avoid chaining methods by reducing the array to a freshly new array of values :slight_smile:

Hope this helps :sparkle:


#3

Thanks that pointed me in the right direction and I looked up how to use Array.prototype.map() method that completed the exercise.

This is what I ended up with SPOILER ALERT.

const squaredIntegers = arr.filter((arr) => Number.isInteger(arr)).map(arr => Math.pow(arr, 2));

I would note that one thing that threw me off guard here was that the dot notation intellisense/autocomplete did not show me the map method in the list of possibilities, and that I therefore thought I was not heading in the right direction. I was a little surprised myself when it worked.