My code is almost completely working but the last output is 0 but it should be 2. What am I doing wrong here? Did I do something wrong in one of the functions? I am lost on this one.
const cakeRecipes = require("./cake-recipes.json");
const searchRecipes = (cakeRecipes, searchCriteria) => {
if (Object.keys(searchCriteria).length === 0) {
return cakeRecipes;
}
return cakeRecipes.filter((recipe) => {
const authors = searchCriteria.authors || [];
const ingredients = searchCriteria.ingredients || [];
const searchTerms = searchCriteria.searchTerms || "";
// Check if the recipe matches all the search criteria
const matchesAuthors = authors.every((author) =>
recipe.Author !== null &&
recipe.Author.toLowerCase().includes(author.toLowerCase())
);
const ingredientsString = recipe.Ingredients.toString();
const matchesIngredients = ingredients.every((ingredient) =>
ingredientsString.toLowerCase().includes(ingredient.toLowerCase())
);
const nameDescription = (recipe.Name + " " + recipe.Description).toLowerCase();
const matchesSearchTerms = searchTerms.split(" ").every((term) =>
nameDescription.includes(term.toLowerCase())
);
return matchesAuthors && matchesIngredients && matchesSearchTerms;
});
};
const printRecipes = (cakeRecipes) => {
console.log(`Number of recipes: ${cakeRecipes.length}\n`);
console.log("-------------");
cakeRecipes.forEach((recipe, index) => {
console.log(`Recipe number ${index + 1}\n`);
console.log(`Name: ${recipe.Name}\n`);
console.log(`Author: ${recipe.Author}\n`);
console.log(`Ingredients: ${recipe.Ingredients.join(', ')}\n`);
});
}
//The output is 0, but it should be 2.
//What am I doing wrong here?
printRecipes(
searchRecipes(cakeRecipes, {
authors: ["best", "cook"],
ingredients: ["syrup"],
searchTerms: "brunch pancakes",
}));
I was expecting the output would be 2 recipes but the output instead was 0.