Divisible by 2?

I couldn’t really figure this out partially because I didn’t know understand why this[i] % 2 was supposed to equal 1. At first I thought it was divided but I guess it’s divisible. Can someone explain this?

Seems 23 is not divisible by 2 so why is it then === 1? (is 1 supposed to measure if it’s true somehow?)

  **Your code so far**

// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
// Only change code below this line
var newArray = [];
for (let i = 0; i < this.length; i++) {
  if(callback(this[i])) {
  newArray.push((this[i]));
}
}
// Only change code above this line
return newArray;
};

var new_s = s.myFilter(function(item) {
return item % 2 === 1;
});
console.log(new_s);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Implement the filter Method on a Prototype

Link to the challenge:

Hey @am93 ,

The % (modulus) operator will take a number and divide it with another number as much as possible. It is a ‘Remainder’ operator as MDN says it. What it does is divide a number and return the remainder of it.

Here’s more about it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder

So, this code is checking if the item divided by 2 will have a remainder of 1. This is not how it test if it’s divisible by 2 (even number). If you want it to test if it’s an even number, you want to use item % 2 === 0.

3 Likes

This just tests if an integer is even or odd. If 2 goes into item cleanly with no remainder then it is even. In this case item % 2 will return 0 (the remainder). If item is odd then item % 2 will return 1 (because there is a remainder of 1).

So return item % 2 === 1 will return true if the integer is odd and false if it is even.

3 Likes

23 / 2 = 11.5
65 / 2 = 32.5

Where is 1? Should I just not think of it as 1 and just think of it as even or odd? (basically not devide) If so, I’d assume it would never not be 1…?

I try not to think like this since I’m just learning but what would the real life use of this be?

Checking if an integer is even or odd is the real life use of % 2.

If you have the integer 3 and divide it by 2 you get 1 (2 goes into 3 once) with a remainder of 1. The modulo operator (%) returns the remainder.

1 Like

Can you do the math on one of these numbers?

I don’t understand 23 % 2 === 1

23 / 2 = 11
remainder = 23 % 2 = 23 - (11 * 2) = 23 - 22 = 1

2 Likes

2 goes into 23 eleven times (2 * 11 = 22) with a remainder of 1.

(2 * 11) + 1 == 23

It’s integer arithmetic, not floating point arithmetic.

2 Likes

I see, thank you. I wish I understood these mozilla docs but I really dont. I guess one day maybe I will…

You perhaps missed the examples section of the documentation?

2 Likes

It will also help if you write out those examples from MDN docs using old school long division.

Then you can visually see the remainder and get better acquainted with the remainder operator.

Like in this example
https://visualfractions.com/calculator/long-division/what-is-5-divided-by-2-using-long-division/

This operator will become handy in coding challenges like Fizz Buzz.

3 Likes

It doesn’t explain it in detail in the MDN docs I guess because remainders are considered a basic maths concept, so google “remainders” or “modulo operation” for more explanation (note that the JS operator % only acts the same as modulo for positive numbers: -11 % 2 is -1 in JS, but -11 mod 2 is 1).

https://www.mathsisfun.com/definitions/modulo-operation.html

:point_up: this has a real-life usecase

:point_up: this has a nice animation

2 Likes

Yeah with this example it’s simple enough to understand. Maybe it’s just me but if you show 100 people the equation 23 % 2 = 1 then I feel most would be confused since for the majority of our life 23 % 2 equals 11.5. Wouldn’t hurt just to mention remainder in the question but yes I understand / and % are different in code

I see what you are getting at, you are saying that ÷ (the division sign) looks like % (the modulo operator). Understood, but when it comes to programming languages the forward slash is used for division and percent for modulo. It’s just something you’ll have to get used to :slight_smile:

2 Likes

Perhaps you confused % and ÷? They look somewhat similar but are very different.

1 Like

Sorry i’m actually struggling with functions more than I thought. Couldn’t solve the next problems so I went back again to try to understand.

How is this referring to var new_s and so checking if var new_s is true?

if(callback(s[i]))

I don’t know why it’s not clicking

Like with myMap, your function myFilter is making a new array. In this case, the function makes a new array that only contains the entries from the old array that return true when passed into the callback function.

Hmm is this the myFilter you’re referring to that contains entries from the myFilter above it?

var new_s = s.myFilter(function(item) {
return item % 2 === 1;
});

I don’t understand because how would the first filter know what is true or not if the new_s hasn’t ran yet

You call the myFilter function with the function that returns true or false as the argument.

new_s is built by running your myFilter function.

What is the criteria determine which is true or false if it’s not new_s?