Write Higher Order Arrow Functions

Write Higher Order Arrow Functions
0.0 0

#1

Tell us what’s happening:
I don’t know if the plugin is not working well or what. This code seems OK to me and verified against other solutions I’ve seen.

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((x) => x%1 === 0);
  squaredIntegers = squaredIntegers.map((x) => x*x);
  // 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 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

your filter callback function logic is wrong.


#3

your logic seems fine… you might try using var or let instead of const for that squaredIntegers variable, seeing as how you’re reassigning it

The value of a constant cannot change through re-assignment, and it can’t be redeclared.


#4

you probably made an oversight in this line
const squaredIntegers = arr.filter((x) => x%1 === 0);
it is x%2 not x%1. For your case the filter was basically doing nothing as every integer in the array list is divisible by 1.


#5

let did the trick for me. thanks a lot


#6

Sounds like you got it figured out, great!

Just a quick aside. The best practice for working with arrow functions is to only use paratheses when you have more than one parameter. See below :slight_smile:

const timesTwo = x => x * 2;  // no parens b/c only one parameter
const multiplyTwoThings = (x, y) => x * y; // parens!

reference