Quotes help make search much faster. Example: "Practice Makes Perfect"

Saturday, May 13, 2017

FreeCodeCamp Diff Two Arrays Sample Solution


function cleanArray(actual) {
  var newArray = [];
  for (var i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i]);
    }
  }
  return newArray;
}

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  var slicedArr1;
  var slicedArr2;
  for(var i=0; i<arr1.length; i++){
    for(var j=0; j<arr2.length; j++){
      if(arr1[i] === arr2[j]){
        //arr1.splice(i,1);
        delete arr1[i];
        //arr2.splice(j,1);
        delete arr2[j];
      }
    }  
  }
  newArr = arr1.concat(arr2);
  return cleanArray(newArr);
}

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

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts