对我的 Javascript 搜索引擎项目的反馈。打印未全部接受结果,未显示错误 [已关闭]

Feedback on my Javascript search engine project. Prints not all accepting result, no errors displayd [closed]

提问人:Yass 提问时间:8/23/2023 最后编辑:Yass 更新时间:8/23/2023 访问量:89

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

3个月前关闭。

这篇文章在 3 个月前被编辑并提交审核,但未能重新打开帖子:

原始关闭原因未解决

[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 的函数,该函数采用配方数组。 • 此功能打印出给定的食谱数量 • 它还应该整齐地打印出食谱列表,显示姓名、作者、”

JavaScript 数组 JSON 过滤器 字符串匹配

评论

1赞 Chris G 8/23/2023
你能在 SO 中分享你的实际代码吗?不使用图片或外部网站链接
0赞 Chris G 8/23/2023
这里的这一行缺少 2 个结尾))return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el));
0赞 Yass 8/23/2023
@ChrisG感谢您的响应:) 我现在已经在 SO 中分享了我的代码。
1赞 Jared Smith 8/23/2023
我不是想粗鲁,我很抱歉它以这种方式出现。我试图给你一些建议,我希望有人在我刚开始的时候给我。“征求反馈有什么问题?”除了太之外,什么都没有。你的代码中总是会有错误(我们都会这样做),如果你每次都必须等待互联网上的人做出回应,你将永远无法完成任何事情。指出这一点怎么不礼貌?
2赞 Yass 8/23/2023
@JaredSmith这是我在 SO 上发表的第一篇文章,在独自工作了很长时间之后。我的第一个JS项目。一遍又一遍地尝试,现在伸出援手。正如许多人在 SO 上所做的那样,包括修复错误。SO不是它的平台。当然,你必须尝试自己做一段时间。自己思考。但是如果你一遍又一遍地尝试,你就可以弄清楚。我在利林体育场。无论如何,你的责任是明确的。一开始,accros有点粗鲁。

答:

-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))))