提问人:Yass 提问时间:8/23/2023 最后编辑:Yass 更新时间:8/23/2023 访问量:89
对我的 Javascript 搜索引擎项目的反馈。打印未全部接受结果,未显示错误 [已关闭]
Feedback on my Javascript search engine project. Prints not all accepting result, no errors displayd [closed]
问:
[JSON 数据配方][1]
'use strict';
const cakeRecipes = require("./cake-recipes.json");
console.log(cakeRecipes[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
// printRecipes(
// searchRecipes(cakeRecipes, {
// authors: ["best", "cook"],
// ingredients: ["syrup"],
// searchTerms: "brunch pancakes",
// })
// ); // 2
//***********part 1: declare function search recipes****//
//Use the JSON.stringify method in this function converting the arrays to a string. This by every part of searching to make it possible to search.
const search = [''];
const result = [''];
function compareArraysWithJSONStringify (arraySearch, arrayResult) {
return JSON.stringify(arraySearch) === JSON.stringify(arrayResult);
}
let compareWithJSONStringify = compareArraysWithJSONStringify (search, result);
//Create a function called searchRecipes that takes all recipes and an object with search criteria.//This function should return an array of recipes that match the search parameters.// method filter() to create new array that filter recipes based on parameters. Push method to add a new item. Returns new array.
//set it to lower case to make it case insensitive
// Creating a function that returns an array of recipes that match the search parameters. Check first if the element is there in the JSON String.
const searchRecipes = (search,recipe) => {
const filterResult = []; // declare empty array.
function filterItems(search, recipe) {
return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el))))
}
};
//************** part 2:ingredients***************//
// First use the JSON.stringify method to convert arrays to string in order to do search matching.
//set it to lower case to make it case insensitive
const filterIngredients = (recipe, search) => {
recipe = JSON.stringify(recipe).toLowerCase()
search = search.map((element) => element.toLowerCase())
const filterResult = searchItemInRecipe(search, recipe)// have to start searching
const result = compareSearchWithResult(search, filterResult) // compare the searching result
return result
}
//***************part 3:author***************//
// First use the JSON.stringify method to convert arrays to string in order to do search matching.
const searchForAuthor = (recipe, search) => {
recipe = JSON.stringify(recipe).toLowerCase()
search = search.map((el) => el.toLowerCase())
let result = false
if (
search.filter((el) => {
if (recipe.includes(el)) {
result = true
}
})
);
return result
}
//************part 4: searchterms*********//
const searchForSearchTerms = (recipe, search) => {
recipe = recipe.Name.concat(recipe.Description).toLowerCase()
search = search.toLowerCase() //
search = search.split(" ")
const filterResult = searchItemInRecipe(search, recipe)
const result = compareSearchWithResult(search, filterResult)
return result
}
// step for searching result recipes and ingredients
const filterRecipes = (recipes, search) => {
const result = []
recipes.forEach((recipe) => {
const ingredientsInRecipe = recipe.Ingredients
const ingrendientsInSearch = search.ingredients
if (ingrendientsInSearch) {
if (!searchForIngredients(ingredientsInRecipe, ingrendientsInSearch))
return
}
//search for author. Check if all is controlled works. Create new array for result using method push() , then return result
const authorFromRecipe = recipe.Author
const authorsFromSearch = search.authors
if (authorsFromSearch) {
if (!searchForAuthor(authorFromRecipe, authorsFromSearch)) return.
}
const searchTerms = search.searchTerms
if (searchTerms) {
if (!searchForSearchTerms(recipe, searchTerms)) return
}
result.push(recipe)
})
return result
}
// Creating a function that takes an array of recipes
const printRecipes = (foundRecipes) => {
const amountOfRecipes = recipes.length
let recipeCount = 1
console.log(
`Number of matched recipes are ${foundRecipes} ` );
//Log the found recipes
recipes.forEach((recipe) => {
const name = recipe.Name
const author = recipe.Author
const description = recipe.Description
const ingredients = recipe.Ingredients
// print out a list of recipes, displaying the name, author(s), description
foundRecipes.forEach((recipe) => {
console.log(`\nRecipe ${recipeCount++}:`)
console.log(`${name}`)
console.log(`By: ${author}`)
console.log("\nAbout this recipe:")
console.log(description)
console.log("\nIngredients:\n")
ingredients.forEach((ingredient) => console.log(` * ${ingredient}`))
console.log("\n______________________________________________\n");
});
})
}
//Paramenters that are used in search
const searchParams = {
authors: ["best", "cook"],
ingredients: ["syrup"],
searchTerms: "brunch pancakes",
}
“首先,在控制零件代码时,它可以工作,但我得到的评论是它不会检查整个字符串。请提供一些指南/在编写代码时如何做得更好?第一个JS项目。
非常感谢!
这是同工项目任务: 创建一个名为 searchRecipes 的函数,该函数采用所有配方和具有搜索条件的对象。此函数应返回与搜索参数匹配的配方数组。 创建一个名为 printRecipes 的函数,该函数采用配方数组。 • 此功能打印出给定的食谱数量 • 它还应该整齐地打印出食谱列表,显示姓名、作者、”
答:
-1赞
rabbibillclinton
8/23/2023
#1
欢迎来到 Stack Overflow!
删除分号并添加两个右括号。
将第 43 行的代码从
return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el));
自
return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el))))
评论
))
return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el));