Diff Two Arrays - HELP! [SOLVED]

Diff Two Arrays - HELP! [SOLVED]
0

#1

how can i return separate elements instead of plain array containing multiple elemens which stick together?
what is wrong with my code?
thank you

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  var temp = [];
  newArr = arr1.concat(arr2);
  var filter = newArr.filter(function() {});

  for (var i = 0; i < newArr.length; i++) {
    if (arr1.includes(newArr[i]) === false || arr2.includes(newArr[i]) === false) {
      temp += [newArr[i]];
    }
  }
  console.log(temp);
  return temp;
}

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


#2

Haiiiyo!

I may not be answering your question properly because I may have misunderstood your question due to your wording. Assuming that “separate elements” means an array (for example, [1, "pikachu", 2, "0", "nyancat", 5]), and that plain array means a string or a number (for example, "mew" and 1205)—the only issue with your code is the following line inside the for loop:

temp += [newArr[i]];

The reason is because addition with arrays actually treat the content inside the arrays as a string, for example:

console.log(["Pika"] + ["chu"]); // Returns "Pikachu", a string
console.log(["Pika"] + "chu"); // Returns "Pikachu", a string
console.log(["10"] + "25"); // Returns "1025", a string
console.log([10] + 25); // Also returns "1025", a string

One common way to add an item into an array is with .push(). I’ll let you figure the rest of it out, good luck! :slight_smile:


#3

great! thank you very much :slight_smile: