I believe my filter is correct, but my map is not. Can you help guide me to the issue?
Thanks
**Your code so far**
const squareList = arr => {
// Only change code below this line
let positiveNumbers = arr
// get positive whole numbers
.filter(number => number > 0 && number % 1 == 0);
// return squared numbers
.map(number => number * number);
// Only change code above this line
};
const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);
// return new arr only with square of positive integers without a decimal
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.
Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem
The function you’ve passed to map is fine, as is the one to filter. However, you’re not returning anything from the function squareList, there’s no return statement anywhere.
Hmm I’ve tried both replacing let positiveNumbers = arr with return arr as well as adding return arr to the bottom but it’s not working. I’m supposed to return arr right?
const squareList = arr => {
// Only change code below this line
return arr
// get positive whole numbers
.filter(number => number > 0 && number % 1 == 0);
// square each whole number
.map(number => number * number);
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
When you divide a number, whether negative or positive, by 1, the remainder is always 0. Since this condition is always true, in effect, your test is
number > 0 && true
You already realized that you don’t need that semicolon after filter. You put a semicolon to terminate a statement. So having that semicolon, the system expects to see the next statement. But it sees a period (.map(number…), which is a syntax error. The expression
arr.filter(...)
returns an array and you apply map to this array, so the correct statement is
Ah, my mindset was fixated with a typed language. I wasn’t thinking that JavaScript is a untyped language and % can work with any values in JavaScript.