Book finder exam

Hi everybody, I’m getting crazy, I’ve been working for 2 days on this and I still can’t fix it!
Here’s my code:
// Click handler for search button
const captureSearchValue = () => {
const searchInput = document.getElementById(“search-bar”).value;
return searchInput.trim().toLowerCase();
};

// Filter books based on search input
const filterBooks = (searchString, bookList) => {
const filteredBooks = books.filter((book) => {
const flattenedValues = flattenObjectValuesIntoArray([book]);
return flattenedValues.some((value) =>
value.toString().toLowerCase().includes(searchString.toLowerCase())
);
});
return filteredBooks;
};

// Empty the book list container, iterate over list of filtered books, return list of books formatted as HTML using the function in helper.js

const structureBooksAsHtml = (filteredBooks) => {
return filteredBooks.map((book) => structureBookAsHtml(book));
};

// Handler triggered when a user clickers the “Search” button. Chains previously defined functions together to filter books based on the search value, formats the books as HTML and renders them to the DOM
const searchBtnClickHandler = (books) => {
const searchValue = captureSearchValue();
const filteredBooks = filterBooks(searchValue, books);
const formattedBooks = structureBooksAsHtml(filteredBooks);

renderBooksToDom(formattedBooks);

};

// Grab search button from the DOM
const searchBtn = document.getElementById(“search-btn”);

// Attach an event listener to the search button
searchBtn.addEventListener(“click”, () => searchBtnClickHandler(books));

and these are the helper functions:

// Flatten object keys into an array so that we search the entire object by the input value
const flattenObjectValuesIntoArray = (arrOfObjs) => {
let flattenedObj;
const flattenedObjsArr = ;
for (let obj = 0; obj < arrOfObjs.length; obj++) {
const objValues = Object.values(arrOfObjs[obj]);
flattenedObj = […objValues.flat()]
flattenedObjsArr.push(flattenedObj)
}
return flattenedObjsArr;
};

// Structure filtered books as HTML and return
const structureBookAsHtml = (book) => {
const bookDiv = document.createElement(“div”);
bookDiv.setAttribute(‘class’, ‘bookDiv’);

const bookTitle = document.createElement(“h2”);
bookTitle.innerHTML = book.title;
bookTitle.setAttribute(‘class’, ‘bookTitle’);

const bookAuthor = document.createElement(“h3”);
bookAuthor.innerHTML = book.author;

const bookTags = document.createElement(“p”);
bookTags.innerHTML = book.tags.join(", ");

bookDiv.append(bookTitle, bookAuthor, bookTags);

return bookDiv;
};

const renderBooksToDom = (elements) => {
const bookListContainer = document.querySelector(“#bookList”);
bookListContainer.innerHTML = “”;

bookListContainer.append(…elements);
};

Almost everything is correct, since it still gives me an error saying “Given a search for 'fiction', expected the bookTitle div to have 6 child elements.”

I really don’t know how to get unstucked, I’m getting crazy and frustrated!

I’m sorry, but we cannot offer any help with exams. (It’s in the guidelines for this forum).

Contact CS if you have curriculum feedback. There’s no one here who can assist with that.

I had a similar issue and I couldn’t understand why my code wasn’t working. I did some debugging and the issue for me was not with my code, it was with the provided helper function: flattenObjectValuesIntoArray (which I didn’t think of checking since it’s already provided…sight)

When I run it on a book object to test it return an EMPTY array :

const book1 = { title: “Wild: From Lost to Found on the Pacific Crest Trail”,
author: “Cheryl Strayed”,
tags: [“nonfiction”, “memoir”, “adventure”, “travel”] };

console.log(flattenObjectValuesIntoArray(book1));
// return:

You can either fix that function or not use it altogether (it’s a requirement to use it, but it doesn’t actually check if you use it and I found it quite easy to get filterBooks to work as expected without using flattenObjectValuesIntoArray.