Why am I getting "null" as extra in output array?

Why am I getting "null" as extra in output array?
0

#1

Tell us what’s happening:

Your code so far

function diffArray(arr1, arr2) {
  var newArr1 = [];
// Same, same; but different.
  
  for (i=0;i<=arr1.length;i++){
  
    
if (arr2.indexOf(arr1[i])==-1){
  newArr1.push(arr1[i]);
}}
   for (i=0;i<=arr2.length;i++){
  
    
if (arr1.indexOf(arr2[i])==-1){
  newArr1.push(arr2[i]);  
}}

  return newArr1;
}

diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.58.

Link to the challenge:



#2

Technically, you are getting undefined instead of null (as the “fake” console shows above the test results). If you use your browser’s console (Ctrl+Shft+J in Chrome) and used surround a particular test in a console.log statement (see below), then you would see undefined values instead of null values

So let’s discuss why you are getting undefined for some of the array elements.

You should already know that array used a zero index, so an array with 4 elements will have a length of 4 and the last index of the array would be 3. Knowing this, do you see anything wrong with both of your for loops’ conditions (shown below)?

i <= arr1.length;

and

i <= arr2.length;

Give up? Click below to see an extra hint.

What will the final value of i be each array? Does an index for that value exist in each array?