Tell us what’s happening:
I have ended up here solving this, can anyone help me, please?
My 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) => arr > 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/
Hey Guys, I got the solution. Use Number.isInteger() method to filter out the integers, here is the solution
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((num) => num > 0 && Number.isInteger(num)).map((num)=> num*num);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
You’ll get better answers if you post any error messages you get, but anyway - you’ve used the variable name arr both on the name of the array you’re filtering as well as within the callback itself. Try changing it to something like arr.filter(a => a > 0)
close to what I got.
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( (num) => num > 0 && num % Number.isInteger(num) === 0).map((num) => num * num);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
my test would not pass without adding the " Number.isInteger(num) === 0
Just to clarify. Number.isInteger() returns a boolean, so:
num % Number.isInteger(num) === 0
translates to: give me the remainder of num divided by boolean equals to 0.
For example, in the first item of the array (4), you’re doing this: return the element with a value of 4 if 4 % Number.isInteger(4) === 0 -> 4 % true === 0 -> 4 % 1 === 0 -> 0 === 0 -> true. In other words, if the value is an integer, it will always be returned.
Having said that, the code you wrote is equivalent to Number.isInteger(num).