filter map | function filterRecipes(recipes, query) {
console.log("début filterRecipes", recipes);
const queryWords = query.toLowerCase().split(" ").filter(function(word) {
return word.length >= 3;
});
filteredRecipes = recipes.filter(function(recipe) {
const name = recipe.name.toLowerCase();
const description = recipe.description.toLowerCase();
const ingredients = recipe.ingredients.map(function(ing) {
return ing.ingredient.toLowerCase();
}).join(' ');
const appliance = recipe.appliance.toLowerCase();
const ustensils = recipe.ustensils.map(function(ust) {
return ust.toLowerCase();
}).join(' ');
return queryWords.every(function(word) {
return name.includes(word) ||
description.includes(word) ||
ingredients.includes(word) ||
appliance.includes(word) ||
ustensils.includes(word);
});
});
console.log("fin filterRecipes", filteredRecipes);
return filteredRecipes;
}
| ready |
while | function filterRecipes(recipes, query) {
console.log("début filterRecipes", recipes);
const queryWords = [];
const words = query.toLowerCase().split(" ");
let i = 0;
while (i < words.length) {
if (words[i].length >= 3) {
queryWords.push(words[i]);
}
i++;
}
filteredRecipes = [];
let j = 0;
while (j < recipes.length) {
const recipe = recipes[j];
const name = recipe.name ? recipe.name.toLowerCase() : '';
const description = recipe.description ? recipe.description.toLowerCase() : '';
let ingredients = '';
let k = 0;
while (k < recipe.ingredients.length) {
ingredients += recipe.ingredients[k].ingredient.toLowerCase() + ' ';
k++;
}
const appliance = recipe.appliance ? recipe.appliance.toLowerCase() : '';
let ustensils = '';
let l = 0;
while (l < recipe.ustensils.length) {
ustensils += recipe.ustensils[l].toLowerCase() + ' ';
l++;
}
let allWordsMatch = true;
let m = 0;
while (m < queryWords.length) {
const word = queryWords[m];
if (!(name.includes(word) || description.includes(word) ||
ingredients.includes(word) || appliance.includes(word) || ustensils.includes(word))) {
allWordsMatch = false;
break;
}
m++;
}
if (allWordsMatch) {
filteredRecipes.push(recipe);
}
j++;
}
console.log("fin filterRecipes", filteredRecipes);
return filteredRecipes;
}
| ready |