Diff Two Arrays (returning commas instead of just number)

Tell us what’s happening:
Currently, my code is returning 4, instead of 4. I would like to ask if anyone knows what is causing the commas and how can we remove them? Thanks!

Your code so far


function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
for(var i = 0;i<=10;i++){
  if(arr1.indexOf(arr2[i])<0){
    newArr.push(arr2[i]);
    
  }
  if(arr2.indexOf(arr1[i])<0){
    newArr.push(arr1[i]);
  }
  
  }
  
  console.log(newArr);
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Your browser information:

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

Link to the challenge:

the commas are not the issue, just a symptom
I added many console.logs to your code to follow each step, and below there is what the console say. Look what happens when i is 4 and below. What do you think can be causing it?
(The difference is the console used, if you use the browser console or resort to using console.log(JSON.stringify(...)) you would see the same thing)

arr1: [1,2,3,5]
arr2: [1,2,3,4,5]
newArr: []

starting loop on i


	i: 0

		arr2[i]: 1
		arr1.indexOf(arr2[i]): 0
		arr2[i] is included in arr1

		arr1[i]: 1
		arr2.indexOf(arr1[i]): 0
		arr1[i] is included in arr2

	i: 1

		arr2[i]: 2
		arr1.indexOf(arr2[i]): 1
		arr2[i] is included in arr1

		arr1[i]: 2
		arr2.indexOf(arr1[i]): 1
		arr1[i] is included in arr2

	i: 2

		arr2[i]: 3
		arr1.indexOf(arr2[i]): 2
		arr2[i] is included in arr1

		arr1[i]: 3
		arr2.indexOf(arr1[i]): 2
		arr1[i] is included in arr2

	i: 3

		arr2[i]: 4
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4]

		arr1[i]: 5
		arr2.indexOf(arr1[i]): 4
		arr1[i] is included in arr2

	i: 4

		arr2[i]: 5
		arr1.indexOf(arr2[i]): 3
		arr2[i] is included in arr1

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null]

	i: 5

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null]

	i: 6

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null,null,null]

	i: 7

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null]

	i: 8

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null,null]

	i: 9

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null,null,null,null]

	i: 10

		arr2[i]: undefined
		arr1.indexOf(arr2[i]): -1
		arr2[i] is not included in arr1, so arr2[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null,null,null,null,null]

		arr1[i]: undefined
		arr2.indexOf(arr1[i]): -1
		arr1[i] is not included in arr2, so arr1[i] is pushed to newArr
		newArr: [4,null,null,null,null,null,null,null,null,null,null,null,null,null]

end loop in i

returned value: [4,null,null,null,null,null,null,null,null,null,null,null,null,null]
1 Like

thank you very much, I understand now!

1 Like