Tell us what’s happening:
My code returns the integers but how can i get the squared value of them? Did my procedures are wrong? Please help me out.
Your code so far
const newArr = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
console.log(newArr.filter((data) => data == Math.floor(data)));
Your browser information:
User Agent is:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
Link to the challenge:
Squaring will not be done in the filter function. In addition to filtering, you will need to modify each value of the array to be replaced by its square (this will happen after filtering).
let newArr = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
let newInt = newArr.filter((data) => data == Math.floor(data));
console.log(newInt.map((data) => data*data));
This code returns the answer…
You may be interested to know that you can in fact “chain” these methods together without needing that intermediate variable in the following way:
newArr.filter( ... ).map( ... );
map have their appropriate arguments. This is part of the strength of these higher order functions, they allow for great power in simple expressions!