Build a Heritage Library Catalog, Step 18

I’m having difficulty passing the test for step 18 of the “Build a Heritage Library Catalog” workshop.

Inside the loop, check if book.year is equal to the string "Unknown". Then, check if grouped["Unknown"] doesn’t exist yet and initialize it as an empty array. Push book into grouped["Unknown"].

Finally, use continue to skip the rest of the loop body. The continue statement jumps directly to the next iteration.

const rawCatalogCards = [
  "From a Buick 8 | King, Stephen | 2002 | Shelf K7",
  "The Shining | King, Stephen | 1977 | Shelf K1",
  "The Stand | King, Stephen | 1978 | Shelf K2",
  "It | King, Stephen | 1986 | Shelf K3",
  "Misery | King, Stephen | 1987 | Shelf K4",
  "Do Androids Dream of Electric Sheep? | Dick, Philip K. | 1968 | Shelf D5",
  "I, Robot | Asimov, Isaac | 1950 | Shelf A8",
  "Foundation | Asimov, Isaac | 1951 | Shelf A9",
  "Dune | Herbert, Frank | 1965 | Shelf H3",
  "Neuromancer | Gibson, William | 1984 | Shelf G8",
  "Snow Crash | Stephenson, Neal | 1992 | Shelf S6",
  "The Martian | Weir, Andy | 2011 | Shelf W5",
  "Ender's Game | Card, Orson Scott | 1985 | Shelf C2",
  "The Hitchhiker's Guide to the Galaxy | Adams, Douglas | 1979 | Shelf A1",
  "Ready Player One | Cline, Ernest | 2011 | Shelf C7",
  "The Dark Tower: The Gunslinger | King, Stephen | 1982 | Shelf K5",
  // edge cases: missing data
  "Unknown Title |  | 1975 | Shelf X1",
  "Mysterious Manuscript | Unknown Author |  | Shelf Z9",
  "Ancient Scroll | Anonymous | 850 | ",
];

function parseCard(rawString) {
  const parts = rawString.split("|");
  const trimmedParts = [];
  for (let i = 0; i < parts.length; i++) {
    trimmedParts.push(parts[i].trim());
  }
  const title = trimmedParts[0];
  const author = trimmedParts[1];
  const year = trimmedParts[2];
  const location = trimmedParts[3];
  return {
    title: title || "Unknown",
    author: author || "Unknown",
    year: year ? parseInt(year) : "Unknown",
    location: location || "Unknown"
  };
}

function parseCatalog(rawCards) {
  const catalog = [];
  for (let i = 0; i < rawCards.length; i++) {
    catalog.push(parseCard(rawCards[i]));
  }
  return catalog;
}

const catalog = parseCatalog(rawCatalogCards);

function findByAuthor(catalog, author) {
  const searchTerm = author.toLowerCase();
  const results = [];
  for (let i = 0; i < catalog.length; i++) {
    if (catalog[i].author.toLowerCase().includes(searchTerm)) {
      results.push(catalog[i]);
    }
  }
  return results;
}

function groupByDecade(catalog) {
  const grouped = {};
  for (let i = 0; i < catalog.length; i++) {
    const book = catalog[i];

    if (book.year === "Unknown") {
      if (grouped["Unknown"] === undefined) {
        grouped["Unknown"] = [];
      }
      grouped["Unknown"].push(book);
      continue;
    }
  }
}

The error displayed is

You should check if grouped["Unknown"] doesn’t exist yet and initialize it as an empty array.

I’ve tried all of the following methods:

    if (book.year === "Unknown") {
      if (!Object.hasOwn(grouped, "Unknown") {
        grouped["Unknown"] = [];
      }
      grouped["Unknown"].push(book);
      continue;
    }
    if (book.year === "Unknown") {
      if (!grouped.hasOwnProperty("Unknown") {
        grouped["Unknown"] = [];
      }
      grouped["Unknown"].push(book);
      continue;
    }
    if (book.year === "Unknown") {
      if (grouped["Unknown"] === undefined) {
        grouped["Unknown"] = [];
      }
      grouped["Unknown"].push(book);
      continue;
    }

They all work. I added a return statement and console.log(groupByDecade(catalog) statements to verify it.

What am I doing wrong?

hello!

Have you tried if(!grouped[“Unknown”])?

:thinking: That did it! It seems wrong, though, after the lessons about truthiness. In another context, !grouped[“Unknown”] would be truthy if the key did exist and was 0, null, or an empty string.

I agree with you. Currently the tests are using Regex, so only this answer is passing the test.

However if you think that the tests should also pass the other methods, you can create a github issue. Thanks for making freeCodeCamp better!

I checked and found out that there is already an open github issue regarding this. So hopefully the tests should be fixed soon.

@sampatee, would you mind adding a comment to that GitHub issue referencing the above? @VAggrippino made an excellent point! IMO, !grouped["Unknown"] shouldn’t be accepted at all unless it’s written like grouped["Unknown"] === undefined.

Will do. I strongly agree with this too.