BETA freeCodeCamp -ES6: Write Higher Order Arrow Functions- Trouble [16, 1764, 36] [SOLVED][SPOILER]

BETA freeCodeCamp -ES6: Write Higher Order Arrow Functions- Trouble [16, 1764, 36] [SOLVED][SPOILER]
0

#1

Hello, i have trouble with this rule:
squaredIntegers should be [16, 1764, 36]

Link: https://beta.freecodecamp.org/en/challenges/es6/write-higher-order-arrow-functions

My code:


"use strict";

const realNumberArray = [ 4, 5.6, -9.8, 3.14, 42, 6, 8.34 ];
let squaredIntegers = realNumberArray.filter((squareSupZero) => squareSupZero >= 0 );
squaredIntegers = realNumberArray.map((squareSupZero) => squareSupZero >=0 );

console.log( squaredIntegers );

It’s maybe a bug with the “const”… I tried changing it to let and even var but i have the same error.

Have a nice day.

ps: if you think it would be better to go to the chat room and ask, tell me !


Write Higher Order Arrow Functions NEED HELP with FILTER,REDUCE function, explain what to be done with these
#3

No bug. Your map function is returning true and false values instead of an array of only the square of the positive integers in realNumberArray. Your are returning:

[ true, true, false, true, true, true, true ]

instead of:

[16, 1764, 36]

If you want to use filter, then make sure that it is capturing not only positive numbers but positive integers. The numbers 5.6 and 8.34 are not integers.

Your filter should return [4, 42, 6]. Then your map function should be on squaredIntegers and not realNumberArray. The map function should square those 3 numbers and return [16, 1764, 36].


#4

Hello, i really thanks you for your help.
I miss readed the exercice. I didn’t see “to compute the square”. And i think using the term ‘float’ would be better than “fractions are not integers”. More direct and easy to understand for foreigners (my thought).

By looking over internet how to identify float from integer, i used this ( x % 1 === 0):

"use strict";

const realNumberArray = [ 4, 5.6, -9.8, 3.14, 42, 6, 8.34 ];
let squaredIntegers = realNumberArray.filter((integer) => integer % 1 === 0 );

squaredIntegers = squaredIntegers.map((square) => square*square );

console.log( squaredIntegers );

Is there a better way to do it directly ? like with Filter itself ? or other already made function ?

Thanks you again.


#5

The reduce function can be used to avoid iterating twice (once for filter and again with map).

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
let squaredIntegers = realNumberArray.reduce((arr,num) => {
  if (num % 1 === 0) arr.push(num*num); // only adds the squared value to arr if a positive integer
  return arr;
}, []); // the [] is used to initialize arr to something in the first iteration

#6

Ok, thanks you ! The use of [] is very special.


#7

I just realized that integer % 1 === 0 is not enough to satisfy the goal of the challenge. If realNumberArray was:

const realNumberArray = [ 4, 5.6, -9, 3.14, 42, 6, 8.34 ]; // replaced the -9.8 with -9

the array returned should still be [16, 1764, 36 ], but without an extra check of integer > 0, squareIntegers would return:

[ 16, 81, 1764, 36 ]

So, going back to your final solution you would need to do:

let squaredIntegers = realNumberArray.filter((integer) => integer % 1 === 0 && integer > 0 );

#8

Ah yes of course :slight_smile: Nicely done.


#9

where is the chat room. i’m new here


#10

#11

I did it this way, but it wants the squaredIntegers to be a const… How would I go about making squaredIntegers a const?

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  let squaredIntegers = arr.filter(num => Number.isInteger(num));
  
  squaredIntegers = squaredIntegers.map(square => square*square);
  
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

#12

Figured it out after a bit of reading. Just had to go straight into the .map function.

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 => Number.isInteger(num)).map(square => square*square);
  
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);


#13

Thats how I solved it too!


#14

a similar solution :slight_smile:

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.map( num => {
    if(Number.isInteger(num)){
       squaredIntegers.push(num * num);
    }
  })
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

#15