Write Higher Order Arrow Functions: .map query

Tell us what’s happening:
I need to include .map() function. I imagine that this needs to occur outside the filter function (and not nested within) but this doesn’t seem to work.

So, my question is two:

  1. Do I need to include a separate .map function outside the parentheses or is it okay to next them?
  2. Am I using % to focus map on non-decimal numerals?

Thank you
Nick

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;
  arr.filter((squareList) => realNumberArray > 0);
  // 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/68.0.3440.106 Safari/537.36.

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

First thing I want to point out.

When you type arr.filter(blah blah blah), you return a filtered array. As it is right now, your code does nothing with that returned, filtered array.

1 Like

To kind of elaborate on zapcannon99’s point, just as we chain String methods (like myString.trim().split(""), for example), we can chain array methods (to continue with the above since the split turned myString toan array, myString.sort(…).join("-") would take that sorted array and perform a join on that.

Logically, can you see an application to your question?

As to your decimal question, how about checking if the given number is equal to itself converted to an integer?

Also, take a look at your filter function. It is not going to do what you expect. Isuggest you look at the filter() docs. The callback in it defines a param that the filter will assin to each member of your arr as it iterates through it, so you create a locally scoped squareList and then never use it.

Your func is passed an array, arr. You can’t rely on using realNumbersArray, as that may not always be your passed parameter…

You’ll gonna need:

  1. Number.isInteger() which determine if number is whole.
  2. After that numbers that are bigger then zero.
  3. Previous two are achievable by using .filter()
  4. After getting filtered array, they you use .reduce()
  5. Product of reduce is what you need to return.
1 Like

I’ve tried a few these suggestions out. I use Number.isInteger(arr) and it doesn’t seem to make that much of a difference. I don’t know if I’m using it correctly. I also used parseInt(arr) which didn’t seem to make a difference. I chained .reduce(arr) to filer and it returned an error. When not returning errors, my code does not return only squares of integers. So I’m not sure if I’m wrong for continuing to use arr or is I should be using a different math function.
Should I even be filtering squareList? I don’t know what else I would use, but I’m wondering if this one of the problems with my code.


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;
  arr.filter((squareList) => Number.isInteger(arr) && arr>0);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

instead:

const squaredIntegers =  arr.filter((x) => Number.isInteger(x)==true && x>0);

As Randal said and i would like to add, look between these two examples and try to understand the difference.

1 Like

Thanks for the example code. That actually put into perspective what was being done. I’m still at an impasse in regard to how to return the squared integers. I have attempted to do so using map. My effort is below. Where am I going wrong?


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((x) => Number.isInteger(x) == true && x>0);
  arr.map(x => x*x);
  

  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

My bad, I was thinking this was the summing one.

But again, arr.map RETURNS a value, and you’re not doing anything with that. In the arr.filter function, the returned value is being stored into squaredIntegers. However, those returned values aren’t what you’re squaring – you’ve gone back to the original array. And squared everything. And not caught the returned value.

That did it. Thank you.