Write Higher Order Arrow Functions map()

Tell us what’s happening:

Hi I am running this code and getting error

undefined is not a function

undefined is not a function

undefined is not a function

undefined is not a function

undefined is not a function

undefined is not a function

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
  let squaredIntegers = [];
  arr.map(findInt)
  let findInt = (item) => {
       if(Number.isInteger(item) && item > 0) {
        squaredIntegers.push(Math.pow(item,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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36.

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

Hello @sadiki,

findInt function doesn’t exist(=== undefined) when arr.map(findInt) because declaration of the function goes after.

It is related to JS engine. It goes through your code 2 times. The first time it reads all the variables and put them to memory without a value. The second time it reads the value.

Try to check this article: https://codeburst.io/js-essentials-the-javascript-engine-302ff38e8465

1 Like

You are calling findInt before you have defined it. This would be fine if you were using the var keyword, but you are using the let keyword.

1 Like

Hi, I defined findInt before calling it and now I am getting this error. I’ve tried to figure out the solution but have failed.
squaredIntegers should be [16, 1764, 36]

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 squaredInteger = [];
  let findInt = (item) => {
       if(Number.isInteger(item) && item > 0) {
        squaredInteger.push(Math.pow(item,2));
       }
  arr.map(findInt)
  
  }
  // change code above this line
  return squaredInteger;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);`Preformatted text`

Hi @sadiki.
map() is not reducing your array.
You have to filter out real numbers and then pow. them. Try to read this about:
filter(),
Number.isInteger()
and
map(),
Math.pow()

This video should help you understand map() link

Happy coding :smiley:

1 Like