0

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.

ECode
  • 3
  • 2
  • 1
    Please provide a proper [mre] of your issue, that includes the necessary sample input data. – CBroe May 24 '23 at 09:46
  • [^^](https://stackoverflow.com/questions/76322148/javascript-searchcriteria-code-not-working-outputting-0-instead-of-2-whats-wr#comment134587006_76322148) For instance, the code above relies on the contents of the top-level `cakeRecipes` variable (read from JSON), which you haven't shown. Please also explain what the code is supposed to do. Why should the result be 2? What are the criteria? Separately, what [debugging](https://stackoverflow.com/questions/25385173/) have you done? – T.J. Crowder May 24 '23 at 09:47
  • `The output is 0, but it should be 2.` depends. Do you have two recipes by Author which includes "best" AND "cook" that includes "syrup" in the Ingredients and "brunch" AND "pancakes" in the Name + Description of thje Recipt? – Jaromanda X May 24 '23 at 10:00
  • as a wild guess, perhaps `authors.every` should be `authors.some` are you searching for multiple authors? then that should do it – Jaromanda X May 24 '23 at 10:01
  • ahh, so it was a logic problem ... `.every` is equivalent to boolean AND, and `.some` is equivalent to boolean OR :p - if you had included some sample data, it would've been obvious (rather than a wild guess) – Jaromanda X May 24 '23 at 10:14

0 Answers0