Search code examples
javascriptarraysjsonfilterstring-matching

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


[JSON data recipes][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",
}

`First when controlling parts codes it works, but I got comment that it does not check the entire string. Please some guidelines/ what to do better in writing my codes? First JS project.

Thank you so much!

This is the assigment project task: // 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. Create a function called printRecipes that takes an array of recipes. • This function prints out the number of recipes it was given • It should also neatly print out a list of recipes, displaying the name, author(s), `


Solution

  • Welcome to Stack Overflow!

    Remove the semi-colon and add two closing parenthesis.

    Change the code on line 43 from

    return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el));

    to

    return search.filter((el) => el.toLowerCase().includes(recipe.toLowerCase().filterResult.push((el))))