I need some thorough explanation

I’ve looked up what filter, map, and reduce does but this is still confusing to me a little. Before I move on to the next challenge I want to understand this completely.

const squaredIntegers = arr.filter((num) => (num) > 0 && num % parseInt(num) === 0).map((num) => Math.pow(num, 2));

Any help?

Thank you,
Silas

I find it really helped, when I was trying to grok the newer changes to javascript (.map(), .filter() or fat arrow functions, to name a few) to expand them out to what I already know. So let’s take the code you’re asking about, and expand it:

const squaredIntegers = arr.filter((num) => (num) > 0 && num % parseInt(num) === 0).map((num) => Math.pow(num, 2));

First, let’s expand that .filter():

let isPositiveInteger = function(num) {
  if (num > = 0 && num % parseInt(num) ){ // so is it positive, and is it an integer?
    return true;
  } else {
    return false;
  }
}

That’s really expanded. Bear in mind that the filter function retains those members that return true from the callback function, and discards those that return false.

So that takes care of that one. Now for the .map() callback function:

let squareIt = function(num){
  return Math.pow(num, 2); // returns num to the power of two, or squared.
}

So now, with those two expanded out, we can put it all together:

// This function returns true for any number that is both positive, and an integer
let isPositiveInteger = function(num) {
  if (num > = 0 && num % parseInt(num) ){ // so is it positive, and is it an integer?
    return true;
  } else {
    return false;
  }
};

// This function takes a number, and returns the square of that number
let squareIt = function(num){
  return Math.pow(num, 2);
}

const squaredIntegers = arr.filter(isPositiveInteger) // use our function above to filter
                           .map(squareIt) // and this one to square each number 
1 Like

You are missing a 0 in the conditional statement of isPositiveInteger (there is written num > =

1 Like

Why is % remainder used? I am confused why it is used.

Silas

1 Like

The reason there is that parseInt makes the number an integer

So if you have 3.1
3.1 % parseInt(3.1) => 3.1 % 3 => 0.1 which is different from 0

If instead it was 3 % 3 (whole number) it is 0

You can also use num % 1 with the same result

The lack of the comparison with 0 in @snowmonkey was confusing me

2 Likes

ohhhhh okay. FINALLY! I’ve been on this for a while and had to look up the solution. I’m glad I understand this now. Thank you guys for explaining this and being really helpful.

Thank you!
Silas

1 Like

Of course, rather than using the modulo operator (the %), we do have Number.isInteger() – does pretty much the same thing in this particular use case. :wink:

1 Like

I so confuse what is a % for and how does 3 = 0.

That is the modulus operator %. 3 is not equal to 0, but 3%3 is equal to 0

The modulus operator returns the reminder of the division between the two numbers, as 3/3 gives exaclty 1 the reminder is 0

Instead 4/3 is 1 with reminder of 1, so if you were to do 4%3 this would give 1

1 Like

If you have a number of things, and you divide them into groups of some other number, how many are left over at the end.

  • You have $10 and 25 cents.
  • You want to divide it up into $1 groupings.
  • That gives you ten $1 groupings, and 25c left over.

1025 % 100 equals 25

If the number divides equally, the calculation equals 0. So for example, if you want to know if a number is even, then % 2 will be 0 if it is:

2 % 2 equals 0
3 % 2 equals 1

2 Likes

oh yeah i have a memory learning that somewhere.

See that’s what I need. dummy it down for me

you have a number of things, and you divide them into groups of some other number, how many are left over at the end.

You have $10 and 25 cents.
You want to divide it up into $1 groupings.
That gives you ten $1 groupings, and 25c left over.

1025 % 100 equals 25

If the number divides equally, the calculation equals 0. So for example, if you want to know if a number is even, then % 2 will be 0 if it is:

2 % 2 equals 0
3 % 2 equals 1

That works for me. but i get:

ES6: Write Higher Order Arrow Functions

It’s time we see how powerful arrow functions are when processing data.

Arrow functions work really well with higher order functions, such as map() , filter() , and reduce() , that take other functions as arguments for processing collections of data.

Read the following code:

FBPosts.filter(function(post) {
return post.thumbnail !== null && post.shares > 100 && post.likes > 500;
})

We have written this with filter() to at least make it somewhat readable. Now compare it to the following code which uses arrow function syntax instead:

FBPosts.filter((post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500)

This code is more succinct and accomplishes the same task with fewer lines of code.

Use arrow function syntax to compute the square of only the positive integers (decimal numbers are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers .

It doesn’t even match up with our problem. Its like if I asked for directions to a party and you give me a map. That is great but as I walking away I see Its a map Of New York and I’m in China. They are both cities yes but it don’t help me to get to the party at all.

I so confuse what is a % for and how does 3 = 0.

num % parseInt(num) === 0

parseInt will return the integer part of a number (it tries to find the first integer). So parseInt(3) will return 3, and parseInt(3.1) will return 3 as well.

If num is 3.1:

num % parseInt(num) === 0;
// so
3.1 % parseInt(3.1) === 0;
// so
3.1 % 3 === 0;
// so
false

If num is 3

num % parseInt(num) === 0;
// so
3 % parseInt(3) === 0;
// so
3 % 3 === 0;
// so
true

Caveat: IMO do not copy the hint answer that uses parseInt: do not use that method to find out if a number is an integer, it is extremely confusing.

JS already has a method for finding out if a number is an integer: Number.isInteger(num)

Number.isInteger(3.1) // false
Number.isInteger(3) // true

If for some reason you don’t want to use that, num % 1 === 0 does the same thing:

If num is 3.1:

num % 1 === 0;
// so
3.1 % 1 === 0;
// so
false

If num is 3:

num % 1 === 0;
// so
3 % 1 === 0;
// so
true

And if you are hellbent on making it similar to the hint, JS provides a trunc function which removes the non-integer part of a number: Math.trunc(num).

So can do either:

num % Math.trunc(num) === 0;
// or
num === Math.trunc(num);
1 Like