Map method not working?

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

Link to the challenge:

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);

what do the tests say? what output do you get?

1 Like

You normally have to return values from functions. Like, if you want to return the string"hello", this won’t work:

const returnHello = () => {
  let hello = "hello";
}

So yes, you have to return the value the function is supposed to be computing.


But the code is never going to reach map. What is at the end of the line with filter on it?

2 Likes

@ilenia was getting unexpected token at .map but didn’t realize it was caused by ; at the end of filter. Thank you for pointing that out @DanCouper

2 Likes

What are you trying to check with this condition?

number % 1 == 0

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

return arr.filter(...).map(...);
1 Like

Thank you for sharing this.

% is actually different than /

I was confused when I first saw this as well but it’s actually divisible by 1 not divided by 1. Heres the doc Remainder (%) - JavaScript | MDN

Right… They are asking why you are checking that the number is divisible by 1 because every integer has a remainder of 0 when divided by 1.

Ah I actually did some research when I was trying to find how to remove the decimals and came across this javascript - Check if a number has a decimal place/is a whole number - Stack Overflow

lol for a second I thought I finally knew something someone didn’t… But I’m actually confused now…

Ah, you’re using modulus as an integer check. That’s what they wanted to know.

It looks like that’ll work for these test cases. I like isInteger a bit better because it won’t work for 10.0, but both work this case.

Yeah @JeremyLT is right Number.isInteger(num) will do the job :smiley:

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.