Tell us what’s happening:
Getting error "cannot read property hasOwnProperty of undefined’

Your code so far

function whatIsInAName(collection, source) {
  var arr = [];
  let arrSource = Object.keys(source)

  // Only change code below this line
  for (let i = 0; i < collection.length; i++) {
      for (let j = 0; j < arrSource.length; j++) {
        if (!collection[i].hasOwnProperty(arrSource[j]) || collection[i][arrSource[j]] !== source[arrSource[j]])  {
          delete (collection[i]) 
  // Only change code above this line
console.log (collection)
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

Hello there.

When you iterate through an array like this:

let arr = [3,4,5];
for (let i = 0; i < arr.length; i++) {

You will get to a point where i = 3, but

arr[0] == 3;
arr[1] == 4;
arr[2] == 5;
arr[3] // Does not exist

Hope this helps

what happens if you check collection[i] after deleting it?

(you get that error)

make so that after you delete it you don’t check that element anymore

i thought after deleting the element in the collection array that matches the condition , then the collection array should not contain it again. but i keep getting that that error “cannot read property hasOwnProperty of undefined” please explain more

Instead of removing things from the input array, what if you add things to the output array? Mutating an object as you iterate over it can be tricky and it’s causing you trouble here.

but since the conditional statements has been initiated there is no way arr[3] will give a value rather it will throw error. in my code above the conditional statement is correct. please explain more

The fact that you have this line in an if statement does not prevent an error from being thrown:


collection[i] does not exist for part of the for loop, because, as others have mentioned, you are mutating the array, so the error you mentioned in your original post gets thrown, because you cannot check the property (perform .hasOwnProperty()) on an item that does not exist.

Hope this helps