Build a Heritage Library - Step 18 correct solution?

Basically, this step asks us to write a function to check if Unknown key exists within grouped object. From previous lessons, I have learned that you should check for existence of keys by doing either grouped.hasOwnProperty(“Unknown”) or Object.hasOwn(grouped, “Unknown”), eventually “Unknown” in grouped, but I’m unsure if you can flip that to check for negative. Either way, my code looks like this

function groupByDecade(catalog) {
  const grouped = {};
  for (let i = 0; i < catalog.length; i++) {
    const book = catalog[i];
    if(book.year === "Unknown") {
      if(grouped.hasOwnProperty(“Unknown”) == false) {
        grouped["Unknown"] = []
      }
      grouped["Unknown"].push(book)
      continue
    }
  }
  return grouped //added for testing purpose
}

I have tried doing either object.hasOwnProperty or Object.hasOwn approach and was rebuffed with “You should check if grouped["Unknown"] doesn’t exist yet and initialize it as an empty array.” debug message. Only after checking through the source of this test I find that it expects if(!grouped["Unknown"]), which I’m not sure if I missed through the chapter, but considering https://www.freecodecamp.org/learn/javascript-v9/lecture-introduction-to-javascript-objects-and-their-properties/how-to-check-if-an-object-has-a-property, I was under impression this is a wrong way of checking this? Am I wrong? Even if I make a wrong assumption here, I’d expect that the other two ways of doing this should also be accepted, especially since it works?

Hi @tepiloxtl,

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

I agree that the tests are too restrictive and any of the following methods of checking for the existence of the property should be accepted:

console.log(!Object.hasOwn(grouped,"Unknown"))
console.log(!grouped.hasOwnProperty("Unknown"))
console.log(!grouped["Unknown"])

Happy coding!