1
\$\begingroup\$

I have made a search engine for recipes. Requirements for this JS Project are as follow:

  1. Create a function called searchRecipes that takes all recipes and an object with search criteria. This should return an array of recipes that match the search parameters

  2. Create a function called printRecipes that takes an array of recipes. The printlist are comment in the first lines . Code runs

  3. If the search object has 2 or more of the 3 possible properties the found recipes should match each of the properties

  4. The object can also have a searchTerms property. The value of the searchTerms property should always be a single string. For a recipe to match all of the given words (search terms) need to be present in either the name or the description of the recipe

  5. A search with this search object should return all recipes that:

    • have "best" OR "cook" as the author
    • AND have "syrup" as an ingredient
    • AND have both "brunch" and "pancakes" in their name or description

Can anyone please review this code and point out what I can do better? While it works (after fixing). For instance it’s concerns JSON data. I do think I have to use JSON.stringify() method for converting the arrays to a string in order to make the search happen for comparing strings`

const cakeRecipes = require("./Yazzrecipes.json");
 //// console.log(YazzRecipes[0]);
 // If you're ready to test: uncomment the code below.
 // printRecipes(searchRecipes(cakeRecipes, {})); // 162
 // printRecipes(searchRecipes(cakeRecipes, { ingredients: ["carrot"] })); // 3
 // printRecipes(searchRecipes(cakeRecipes, { authors: ["Good food"] })); // 32
 // printRecipes(searchRecipes(cakeRecipes, { searchTerms: "christmas simple" })); // 5
 // printRecipes(
 // searchRecipes(cakeRecipes, {
 // ingredients: ["nuts"],
 // searchTerms: "christmas simple",
 // })
 // ); // 2
 /* Parameters 
 const searchParams = {
 ingredients: ["carrot", "butter"],
 }; */
 // Creating a function that returns an array of recipes that match the search parameters
 const searchRecipes = (recipes, searchItem) => { 
 let ResultMatch = [].concat(recipes);
 if ("ingredients" in searchItem) {
 ResultMatch = ResultMatch.filter((recipe) => {
 const ingredientsInRecipe = recipe.Ingredients.toString();
 return searchItem.ingredients.every((ingredient) =>
 ingredientsInRecipe.includes(ingredient.toLowerCase())
 );
 });
 }
 if ("authors" in searchItem) {
 ResultMatch = ResultMatch.filter((recipe) => {
 return searchItem.authors.some((author) => {
 if (Object.is(recipe.Author, null)) {
 return false;
 } else {
 const recipeAuthor = recipe.Author.toLowerCase();
 return recipeAuthor.includes(author.toLowerCase());
 }
 });
 });
 }
 if ("searchTerms" in searchItem) {
 ResultMatch = ResultMatch.filter((recipe) => {
 const searchTerms = searchItem.searchTerms.split(" ");
 return searchTerms.every((searchTerm) => {
 if (Object.is(recipe.Description, null)) {
 const recipeName = recipe.Name.toLowerCase();
 return recipe.Name.toLowerCase().includes(searchTerm.toLowerCase());
 } else {
 const recipeDescription = recipe.Description + " " + recipe.Name;
 return recipeDescription
 .toLowerCase()
 .includes(searchTerm.toLowerCase());
 }
 });
 });
 }
 return ResultMatch;
 };
 // Creating a function that takes an array of recipes
 const printRecipes = (foundRecipes) => {
 console.log(
 `Result of ${foundRecipes.length} that have been found as matches.\n\n______________________________________________\n`
 );
 // A listed display of the recipe, author(s), description and ingredients
 foundRecipes.forEach((recipe) => {
 console.log(`Recipe: ${recipe.Name}\n`);
 console.log(`Author(s): ${recipe.Author}\n`);
 console.log(`Description: \n${recipe.Description}\n`);
 console.log(`Ingredients:`);
 recipe.Ingredients.forEach((ingredient) => console.log("- " + ingredient));
 console.log("\n______________________________________________\n");
 });
 };
 // Printing parameters used for searching
 printRecipes(
 searchRecipes(cakeRecipes, {
 ingredients: ["syrup"],
 author: ["best", "cook"],
 searchTerms: "brunch pancakes",
 })
 );

JSON file read

Sᴀᴍ Onᴇᴌᴀ
29.6k16 gold badges45 silver badges203 bronze badges
asked Aug 24, 2023 at 14:58
\$\endgroup\$
0

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.