Problem understanding modulu (%)

Please help me understand the code.
I have this solution I googled, but can’t understand the returned value.

function findOdd(numbers) {
  var count = 0;
  for(var i = 0; i<numbers.length; i++){
    for(var j = 0; j<numbers.length; j++){
      if(numbers[i] == numbers[j]){
        count++;
      }
    }
    if(count % 2 != 0 ){
      return numbers[i];
    }
  }
};

console.log(findOdd([1,1,1,1,1,1,10,1,1,1,1])); //10

The answer is 10 but I can’t understand the code.

Here is what I know so far, there is a loop inside the loop meaning there is 2 loops happening.
The if statement inside the loop is comparing the elements of the same array.:
if(numbers[i] == numbers[j])
IF:
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
10 == 10 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true

All is true meaning that count++; will be 11.

Now this code

    if(count % 2 != 0 ){
      return numbers[i];
    }

if 11 % 2 != 0 or 1 != 0 // true
it is true so it should return numbers[i] ?

What I understand is that it should return [1,1,1,1,1,1,10,1,1,1,1]. because (count % 2 != 0 ) is true .

Please help me understand this. Thank you

Hi @bradrar1 ,
First i could be wrong, but i happen to think the name of this function is totally misleading. It is not really finding oddnumbers but rather finding which element appears an odd number of times in the array.

In this case you get 10 because the number 10 appears 1 time in the array, while 1 appears 10 times. the for loop with the ( i) checks one element against all other (j) elements in the inner for loop. Meaning each time i has one value, it checks that one value against all j values before moving on to the next i value.

For the first loop for example, when i=0, num[0] = 1, it will go through all values of (j) while (i) remains the same.
outer i loop against inner j loop
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 10 // false
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 appears 10 times and is an even number of times

while at the index 6. i=6 hence num[6] = 10
outer i loop against inner j loop
10 == 1 // false
10 == 1 // false
10 == 1 // false
10 == 1 // false
10 == 1 // false
10 == 1 // false
10 == 10 // true
10 == 1 //false
10 == 1 // false
10 == 1 // false
10 == 1 // false
and here 10 appears 1 time which is an odd number of times, so it returns the value 10.

2 Likes

I agree with @lulets

If you need get array of odd numbers:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
const oddNumbers = numbers.filter(n => n % 2 !== 0)
console.log(oddNumbers) // [1, 3, 5, 7, 9, 11]

No.
First iteration of loop:

i = 0, numbers(i) = 1

1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 10 // false
1 == 1 // true
1 == 1 // true
1 == 1 // true
1 == 1 // true
count is 10, so
count % 2 != 0 // false

1 Like

Sorry for my question because I don’t understand what you mean by outer i loop against inner j loop.

I don’t understand why at num[6] is still 1 while its comparision is 10 .
when in the code if(numbers[i] == numbers[j]) is basically the same thing?

Please correct me if I am wrong.

Thank you

You can add

console.log('i is "' + i + '", numbers[i] is "' + numbers[i] + '", j is "' + j + '", numbers[j] is "' + numbers[j] + '"')

just above the if statement to see that when i is 0, j will assume values from 1 to numbers.length, then when i is 1, j will again assume all values from 0 to numbers.length etc

Try also visualizing your code execution with this:
http://pythontutor.com/javascript.html

Things don’t exactly happen how you are thinking

1 Like

So the Modulo / Remainder operator has always been a bit mysterious thing. But the gist is when you do something like 11 % 2 === 1 you get back 1? 11 / 5 === 5.5 so shouldn’t it be 5 not 1? Seeming as it suppose to return the remainder. but the important part to use is that it returns 0 if you had something like 12 % 2 === 0 because 12 / 2 has no remainder.

Its honestly makes little sense the values you get back from it, so if there is one operand in javascript I don’t like above others it is this one.

if you divide 11 by 2, the result is 5 with a reminder of 1. The Modulus/Remainder operator returns that reminder.
11 / 5 is 2 with reminder of 1
12 / 2 is an exact division, it is exaclty 6, so the reminder is 0

In practice, it runs a division so that the result is a whole number and return whatever it remains

6.6 / 1 the result is 6 with reminder 0.6

You can also see it as
(note: they give the same result only with a whole divisor)

const reminder = function(dividend, divisor) {
   let reminder = dividend;
   while (reminder >= divisor) {
      reminder -= divisor;   
   }
   return reminder;
}

in this case reminder(4, 3) gives the same result as 4 % 3, 1

1 Like

@bradrar1 When i said outer loop and inner loop, i meant to highlight that the i and j each count differently. For every one time the i loop executes, the j loop executes completely because it is inside the i loop. Note that the comparison is done inside the j loop, therefore while j is executing, the value of i at that time is always the same - it is the value [i] had just before the j loop began its execution and does not change until the j loop finishes its execution. The only thing that changes inside the j loop is numbers[j]

So lets say when i = 0 it will stay constant until the j loop finishes its own execution from j = 0 to j<numbers.length. Once j completes its execution, i then adds one and moves to i = 1 and the process is repeated this time with a different value of i.

When i is 0, then the statements to be compared are numbers[0] against numbers[j = 0 to j = last]. From that numbers[0] is always going to be 1 and numbers[j = 0 to j = last]. So 1 will compare itself to all values of j, which is all values of the array. So when j = 6, i is still i = 0 therefore comparison will be is 1 == 10, which is false.

However when i is finally at index 6, comparison will now be numbers[6] against numbers[j=0 to j=last]. At numbers 6 numbers[6] = 10, so 10 will compare itself to all values of j.

Hope that helps!

1 Like

@lulets I am understanding it better thanks to your explaination and with the help of @ilenia’s comment : http://pythontutor.com/javascript.html#mode=display

I only have one last question because now I understand how inner i and outer j works.
my question is in this loop.

 if(numbers[i] == numbers[j]){
        count++;
      }

Let’s say that i is 0 and j is 1. so

if ( 0 == 1 ) { //this is false
  count++   //but count++ activates?
} 

I used http://pythontutor.com/javascript.html and count++ is triggered. this is the same with every other comparison with i and j.

thus at numbers 6 numbers[6] = 10 the value of count is 61. this is my question. Thanks

i is 0 and j is 1 but the comparison isn’t between i and j but between numbers[0] which is 1, and numbers[1] which is 1. So the true is correct

1 Like