Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
3
Indexable
Never
function functionnalSearchAlgorithm(keyword, recipes) {
  const result = recipes.filter(function isRecipeMatching(recipe) {
    const lowerCaseName = recipe.name.toLowerCase()
    const hasNameMatchingKeyword = lowerCaseName === keyword;
    if (hasNameMatchingKeyword === true) {
      return true;
    }
    
    const lowerCaseDescription = recipe.description.toLowerCase()
    const hasDescriptionMatchingKeyword = lowerCaseDescription === keyword;
    if (hasDescriptionMatchingKeyword){
      return true
    }
    
    const hasIngredientMatchingKeyword = recipe.ingredients.some(function isMatching(ingredient) {
      const lowerCaseIngredient = ingredient.toLowerCase() // some sert a verifier si sa retourne au moins un ingredient
      const hasIngredientMatchingKeyword = lowerCaseIngredient === keyword;
      return hasIngredientMatchingKeyword;
    }) 

    if (hasIngredientMatchingKeyword) {
      return true;
    }

    return false; // si la recette ne correspond pas
  })

  return result;
}