I have made a search engine for recipes. Requirements for this JS Project are as follow:
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 parametersCreate a function called
printRecipes
that takes an array of recipes. The printlist are comment in the first lines . Code runsIf the search object has 2 or more of the 3 possible properties the found recipes should match each of the properties
The object can also have a
searchTerms
property. The value of thesearchTerms
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 recipeA 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",
})
);