@Klaudia First you’ll want to filter() positive integers (fractions are not integers). Then, once you have an array of these integers, you can use map() to square each integer and return the result.
Note: it is possible to chain functions like arr.filter().map();.
Does that help? I had trouble with this one when I first came across it. You can see my thoughts about it in this forum topic reply.
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
“use strict”;
// change code below this line
squaredIntegers([16, 1764, 36]);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
I put that code but it is tell me:
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
squaredIntegers is not a function
Have you declared your squaredIntegers() function? When I look at your code sample above I don’t see where you declared a const or let variable which contains squaredIntegers nor a declared function
In fact, I see that squaredIntegers is being called before it is being declared:
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
“use strict”;
// change code below this line
squaredIntegers([16, 1764, 36]); //<---Where it is called
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray); //<--- Where it is declared
console.log(squaredIntegers);
Maybe a better route would be creating the logic to sort through which numbers are integers and squaring first.
map, filter, or reduce should be used
but I didn’t know where to put it
However, do you understand why that is being used? Would you be able to explain what is happening through each of those array methods and why they are chained the way they are?
I understand the theory of how this excercise works but can’t figure out how the syntax works. I have used the filter function to loop through the realNumbersArray to find whole numbers. How can I use the maps function to square the result? I
Any help would be much appreciated
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 squaredIntegers = arr.filter(num => {
return num % 1 === 0;
//how do I incorperate the map() function?
});
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);