Issue with FCC exercise "Wherefore art thou"

I am not clear as to why the exercise works if I take “!obj.hasOwnProperty(arr[i]” out of the if statement. It’s just an “artifact” to return false and thus keep looping?

function whatIsInAName(collection, source) {
  const arr = Object.keys(source)
  
  return collection.filter(function(obj) {
    for (var i = 0; i < arr.length; i++) {
      console.log(!obj.hasOwnProperty(arr[i]))
      console.log(obj[arr[i]])
      console.log(source[arr[i]])
    if ( obj[arr[i]] !== source[arr[i]]
    ) {
      return false
    }
    }
    console.log(obj)
    return true
      
  })
}

//whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
//whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })

Challenge:

Formatted code:

function whatIsInAName(collection, source) {
  const arr = Object.keys(source)

  return collection.filter(function (obj) {
    for (let i = 0; i < arr.length; i++) {
      console.log(!obj.hasOwnProperty(arr[i]))
      console.log(obj[arr[i]])
      console.log(source[arr[i]])
      if (obj[arr[i]] !== source[arr[i]]) {
        return false
      }
    }
    console.log(obj)
    return true

  })
}

//whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
//whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })

Where did you have !obj.hasOwnProperty(arr[i])?

Sorry.

function whatIsInAName(collection, source) {
  const arr = Object.keys(source)
  
  return collection.filter(function(obj) {
    for (var i = 0; i < arr.length; i++) {
      console.log(!obj.hasOwnProperty(arr[i]))
      console.log(obj[arr[i]])
      console.log(source[arr[i]])
    if (!obj.hasOwnProperty(arr[i]) || obj[arr[i]]  !== source[arr[i]]
    ) {
      return false
    }
    }
    console.log(obj)
    return true
      
  })
}

//whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
//whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })

Ah, I just edited this solution. It isn’t a great solution.

It might be handy to compare this to your solution. What did you write for this challenge?

The gist is that if the property doesn’t exist, then it can’t possibly have the same value as the source.

Well, I am still working on my solution. But the one I showed you is Solution #1 for the exercise I think.

Ah. I wouldn’t look at the solutions until you have written your own. The point isn’t to get code that makes the check marks but to practice solving problems. You will run out of solutions to look at pretty quickly as you get to more complicated problems, so you’ll need the practice from these problems.

ok. Got it. I have to write my own code no matter how hard and long it takes. But why would FCC publish a solution that is not a good one according to what you said earlier in the thread?.

To be clear, I totally think getting help is good. But it’s much better to get help by asking questions than looking up answers. You get better practice that way.

All of our guide post solutions are user suggested. They don’t always get updated with modern, clearer syntax as Javascript is updated.

Good tips. Thank you.

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