If statement inside a For Loop. QUESTION

If statement inside a For Loop. QUESTION
0.0 0

#1

I am trying to figure out the result of this code:

function diffArray(arr1, arr2) {
   var newArr = []; 

   var ordArr = arr1.concat(arr2).sort();  //[1,1,2,2,3,3,4,5,5]

   for ( var i = 0;  i < ordArr.length ; i++ ) {
       if (ordArr[i]  !==  ordArr[i+1] ){

       newArr.push(ordArr[i]);
       }
   }
   console.log(newArr);
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

This code then console logs the new array of the variable newArr which is

0 : 1
1 : 2
2 : 3
3 : 4
4 : 5

This is where I’m confused. I expected the result of this code to be:

0 : 1
1 : 1
2 : 2
3 : 2
4 : 3
5 : 3
6 : 4
7 : 5
8 : 5

Why did I think of this? Here is my thought process.

  1. So first I created a variable named newArr which is an empty array.

  2. Then I concatenated arr1 and arr2 then sorted it in growing order and put it in the variable ordArr which will return [1,1,2,2,3,3,4,5,5].

3.Now I created a For Loop

  1. Inside that For Loop is an If Statement This is how I read this If Statement… If ordArr[i] is not equal value or equal type of ordArr[i+1] then you will push ordArr[i] into the variable newArr (this is where I don’t really get it.)

  2. I then console.log newArr.


I reread the definition of If Statement and this is how I understand it.

Use **if** to specify a block of code to be executed, if a specified condition is true
if (condition) {
   block of code to be executed if the condition is true
}

In my code. The condition is true so I executed newArr.push(ordArr[i]). And ordArr[i] is

0:1
1:1
2:2
3:2
4:3
5:3
6:4
7:5
8:5

But then again. Why is the result different from what I expect it to be?
(Please tell me what piece of information I’m missing or where I’m wrong in my thought process ).

All comments are welcome.

I’m experimenting with this code in order to answer this challenge at FCC : https://www.freecodecamp.org/challenges/diff-two-arrays.


#2

If the value of the current item is not equal to the value of the next item, push the current item into the new array.

So

[ 1 // do not push: value is 1, next value is 1
, 1 // push: value is 1, next value is not 1
, 2 // do not push: value is 2, next value is 2
, 2 // push: value is 2, next value is not 2
, 3 // do not push: value is 3, next value is 3
, 3 // push: value is 3, next value is not 3
, 4 // push: value is 4, next value is not 4
, 5 // do not push: value is 5, next value is 5
, 5 // push: value is 5, next value is not 5
]

#3

Adding to what @DanCouper explained above.


#4

Thanks @DanCouper and @randelldawson for the explanation. I now understand why this is the result.


#5

@randelldawson, How do I console log that on what you did with Repl.it? I want to know what is the explanation if I add this in my If Statement.

 if(ordArr[i] !== ordArr[i+1] && ordArr[i] !== ordArr[i-1]){
       newArr.push(ordArr[i]);
       }

The result is 4.

What to push and not push?


#6

The change to your if statement works and will solve the challenge.

If you are asking how to console.log on Repl.it, you can simply click on the “open in repl.it” link in upper right-hand corner of the link I posted above and add you own console.log statements.

Also, you can add the console.log statements on the FCC site, but you will have to use your browser’s console (Ctrl+Shft+J in Chrome) to see the actual console.