myFunction2 returning the wrong values

Tell us what’s happening:
It seems that myFunction2 returns the wrong values. It should take the element from onlyArr and check if it is present in args. If not, it should add that element to result array. Pls, help.

  **Your code so far**

function destroyer(arr) {
var args = [...arguments]; 
var onlyArr = args.shift(); 
var result = [];

// Checking if args are in onlyArr
args.forEach(myFunction); 
function myFunction(item) {
  if (!(item in onlyArr)) {
    result.push(item); 
  }
}

// Checking, if elements in onlyArr are in args
onlyArr.forEach(myFunction2);
function myFunction2(item) {
  if (!(item in args)){
    result.push(item); 
  }
}

console.log("Result:" + result);
return result;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3, 8);
  **Your browser information:**

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

Challenge: Seek and Destroy

Link to the challenge:

The in operator is for checking if a key exists on an object. It does not check if a value is found in an array.

Thank you, but then, why it si working with myFunction? It’s the same principle applied to the same type (array)…?

Well, the keys of an array are it’s indexes, which are numbers themselves. I think it could just be working by chance.

1 Like

Thank you, I’ve just solved it with indexOf() method :slight_smile:

Nice! There’s also includes() which is probably a little more intuitive.

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