Algorithms - Find the Symmetric Difference

Tell us what’s happening:
Describe your issue in detail here.
Can somebody explain why the test comparing elements in the second array passed into this function as args, if ((arguments[1][i] in arguments[0]) === false),
works by adding [5, 4] to symDif1 while the other test , ((arguments[0][i] in arguments[1]) === false) creates an empty array ? The contents of both arguments[0] and arguments[1] are identical for both if statements, but the comparison of the first array to the second produces an empty array instead of [3] as expected.

  **Your code so far**
function sym(args) {
console.log(arguments.length);
console.log(arguments[0], arguments[1]);

let symDif1 = [];
for (let i = 0; i < arguments[1].length; i++) {
  if ((arguments[1][i] in arguments[0]) === false) {
    symDif1.push(arguments[1][i]);
  }
}
console.log(symDif1);

console.log(arguments.length);
console.log(arguments[0], arguments[1]);

let symDif2 = [];
for (let i = 0;  i < arguments[0].length;  i++) {
  if ((arguments[0][i] in arguments[1]) === false) {
    symDif2.push(arguments[0][i]);
  }
}
console.log(symDif2);

return args;
}

sym([1, 2, 3], [5, 2, 1, 4]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Algorithms - Find the Symmetric Difference

Link to the challenge:

Its bugged because the in keyword checks if the index is in the array, not the value. Try it out like this:
console.log(3 in [5, 2, 1, 4]); //returns true because the array has index from 0 - 3
console.log(4 in [5, 2, 1, 4]); //returns false because the array has index from 0 - 3

Got it. Thanks. I was going to switch to includes() but I wanted to understand what was happening with in first. I think I was confusing it with using in in a Python for statement.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.