# Diff-two-arrays(can't debug non-working algorithm)

**URL:** <https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015>\
**Category:** General\
**Created:** [May 24, 2016, 2:13pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015 "2016-05-24T14:13:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameToReachPeople](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nametoreachpeople/32/10814_2.png) [@nameToReachPeople](https://forum.freecodecamp.org/u/nameToReachPeople)\
**Post date:** [May 24, 2016, 2:13pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/1 "2016-05-24T14:13:32Z")

</div>

Challenge: challenges/diff-two-arrays

> **[Learn to code with free online courses, programming projects, and interview...](https://www.freecodecamp.org/challenges/diff-two-arrays)**
>
> Learn to code with free online courses, programming projects, and interview preparation for developer jobs.

```
function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.//JF
  console.log("arr1: " + arr1 + " arr2: " + arr2);
  
  for ( var i = 0; i < arr1.length; i++ ){
    
    var oldLength;
    oldLength = arr2.length;
    arr2=arr2.filter(filterer, arr1[i]);
    if ( arr2.length !== oldLength)
      arr1[i] = null;
    
  }
  
  
  arr1=arr1.filter(filterer, null);
  console.log("second----- arr1: " + arr1 + " arr2: " + arr2);
  return arr1.concat(arr2);
}

function filterer(value){
  
  if ( this !== value )
    return true;
    
}

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

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 24, 2016, 3:58pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/2 "2016-05-24T15:58:35Z")

</div>

A good first step is to add in hella `console.log()` statements to identify where things aren’t what you expect them to be (when you’re developing a full project you’ll use debugging with breakpoints to accomplish this).  
[Here is a link to your code with more logging.](https://repl.it/CUaM/1)

Now we can see that the problem is that both `this` and `value` have the type of `object`. With objects, I believe that strict equality requires the two objects to have the same reference (they are two variable names referring to the same object) as opposed to by value.  
[Here is a link to your code without strict inequality.](https://repl.it/CUaM/2)

Now we can see that there is still a problem with your approach: instead of returning the array `['pink wool']` we have an array where all the duplicated values are replaced with `null`.  
I’ll leave you to look for the logic error that is causing that problem.

Happy coding!

---

<div class="post-metadata">

**Author:** ![ChadKreutzer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chadkreutzer/32/159254_2.png) [@ChadKreutzer](https://forum.freecodecamp.org/u/ChadKreutzer)\
**Post date:** [May 24, 2016, 4:04pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/3 "2016-05-24T16:04:26Z")

</div>

+1 to the console.log love. I’m just now starting to learn testing, but I’m all about putting console.log all over when I’m trying to figure out what’s going wrong.

---

<div class="post-metadata">

**Author:** ![nameToReachPeople](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nametoreachpeople/32/10814_2.png) [@nameToReachPeople](https://forum.freecodecamp.org/u/nameToReachPeople)\
**Post date:** [May 24, 2016, 5:43pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/4 "2016-05-24T17:43:29Z")

</div>

Very good explanation, and for learning purpose “abstract” 🙂

---

<div class="post-metadata">

**Author:** ![nameToReachPeople](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nametoreachpeople/32/10814_2.png) [@nameToReachPeople](https://forum.freecodecamp.org/u/nameToReachPeople)\
**Post date:** [May 25, 2016, 4:53pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/5 "2016-05-25T16:53:52Z")

</div>

I have read here: [https://www.toptal.com/javascript/10-most-common-javascript-mistakes](https://www.toptal.com/javascript/10-most-common-javascript-mistakes)

this: “(== and != automatically perform type conversion when comparing two things, whereas === and !== do the same comparison without type conversion.)”.

I’m not sure yet why i need to use “!=” instead “!==”? When, `this` is digit as, `value` in `filterer`?

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 25, 2016, 5:33pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/6 "2016-05-25T17:33:47Z")

</div>

It’s because we’re looking at objects.  
[Here is a link to your code](https://repl.it/CUaM/3) with `this` and `value` converted to strings before comparison.

As per [MDN’s page on Comparison operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators):

> Two distinct objects are never equal for either strict or abstract comparisons.

---

<div class="post-metadata">

**Author:** ![nameToReachPeople](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nametoreachpeople/32/10814_2.png) [@nameToReachPeople](https://forum.freecodecamp.org/u/nameToReachPeople)\
**Post date:** [May 28, 2016, 3:32pm UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/7 "2016-05-28T15:32:13Z")

</div>

Can u explain me this code:

```
  var a1 = ["yes"];
  var a2 = ["yes"];
  
  var a3 = ["yes", "yes"];

  return a3[0] === a3[1]; // return true
  return a1[0] === a2[1]; // return false
```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 29, 2016, 2:45am UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/8 "2016-05-29T02:45:37Z")

</div>

You have an error. `a2[1]` is `undefined`.

```auto
  var a1 = ["yes"];
  var a2 = ["yes"];
  
  var a3 = ["yes", "yes"];
  
  console.log("a1[0]" + ": " + a1[0] + ": " + typeof a1[0]); // a1[0]: yes: string
  console.log("a2[1]" + ": " + a2[1] + ": " +typeof a2[1]); //a2[1]: undefined: undefined

  console.log(a1[0] === a2[1]); // false - because "yes" !== undefined

```

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 3:46am UTC](https://forum.freecodecamp.org/t/diff-two-arrays-cant-debug-non-working-algorithm/4015/9 "2021-01-16T03:46:51Z")

</div>

Closed on January 16, 2021, 3:46am UTC


