Build a Heritage Library Catalog - Step 14

Tell us what’s happening:

I coded the step correctly, but it keeps saying I’m wrong?
I don’t understand

Your code so far

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++) {
    //let myTrial = catalog[i].toLowerCase().includes(searchTerm)
    console.log(parseCard(catalog[i]));
// User Editable Region
    if(parseCard(catalog[i]).author.toLowerCase().includes(searchTerm)){
      //results.push(catalog[i]);
      results.push(parseCard(catalog[i]));
    }    
  }
  console.log(results)
  return results;
// User Editable Region
}

console.log(findByAuthor(rawCatalogCards, "king").length);
console.log(findByAuthor(rawCatalogCards, "asimov").length)

Your browser information:

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

Challenge Information:

Build a Heritage Library Catalog - Step 14

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-heritage-library-catalog/69a9de7ceb9b18f4fc988306.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @carl4christ.22,

Why are you using parseCard? What is the purpose of that function?

Happy coding

Well, step 14 says we should code the following inside the loop: check whether the current entry’s author (converted to lowercase) includes searchTerm. The .includes() method returns true if a string contains another string as a substring. If the condition is true, push the entry into results. After the loop, return results.

I used parseCard because it returns an object with the author property. Without parseCard, the code could still work because the includes can search the catalog even if it’s a string.

Initially I didn’t use the parseCard function. And it didn’t accept the code I submitted

Don’t you have a global variable that was assigned a value using parseCard?

Isn’t findByAuthor already looping over that global variable?

There was a global variable that used the parseCard function, yes. But I think that was a previous step. And some of the recent steps before step 14 said to delete the variable ad the console log lines that were required after it.

I think the global variable in that code I posted uses parseCatalog for now (before the definition of the findByAuthor function.

Even without the parseCard or parseCatalog functions, the code is still not accepted - and I don’t understand why it keeps being rejected

Like I said, even without the parseCard or parseCatalog functions, the code is still rejected. This is the code I wrote without using both functions in this 14th step:

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++) {

//let myTrial = catalog\[i\].toLowerCase().includes(searchTerm)

console.log(catalog\[i\]);

if(catalog\[i\].includes(searchTerm)){

  //results.push(catalog\[i\]);

  results.push(catalog\[i\]);

}    

}

console.log(results)

return results;

}

When I run the code, in the console, I keep seeing these error messages:

// running tests

  1. findByAuthor(catalog, "king").length should be 6.
  2. findByAuthor(catalog, "asimov").length should be 2.
  3. findByAuthor(catalog, "xyz").length should be 0.
    // tests completed

I don’t understand why my code keeps getting rejected.

…check whether the current entry’s author (converted to lowercase) includes searchTerm

Are you doing this?

Yes. After your reply, I added the toLowerCase function
See:

function findByAuthor(catalog, author) {

const searchTerm = author.toLowerCase();

const results = [];

for (let i = 0; i < catalog.length; i++) {

//let myTrial = catalog\[i\].toLowerCase().includes(searchTerm)

console.log(catalog\[i\]);

if(catalog\[i\].toLowerCase().includes(searchTerm)){

  //results.push(catalog\[i\]);

  results.push(catalog\[i\]);

}    

}

And I keep getting this:
// running tests

  1. findByAuthor(catalog, “king”).length should be 6.
  2. findByAuthor(catalog, “asimov”).length should be 2.
  3. findByAuthor(catalog, “xyz”).length should be 0.
    // tests completed

What is it your lowercasing — the whole catalog object??

catalog is an array - an array of strings, right? So, the instruction said to check whether the current entry’s author (converted to lowercase) includes searchTerm, inside the for loop.

To answer your question, I am lowercasing the current entry of the catalog string, that the loop is iterating over. That’s why I used catalog[i]

Is it? Try logging catalog beneath the global assignment. What do you see in the console?

Oh snap :grinning_face:

It works now. I think I figured out what the issue was. I think the issue was I was treating catalog as a string, whereas it is an object from the global variable.

Thank you for your assistance :grin: