When comparing the arrays in bakeryA and bakeryB with the ingredients of each recipe, the chooseRecipe function should print the name of the recipe if both bakeries have an ingredient for it. In this scenario, the output should be Persian Cheesecake. However, the function currently returns an empty array.
Even though I'm initializing suitableRecipe as an empty array, shouldn't suitableRecipe.push(recipes[i].name); take care of that?
I would appreciate any suggestions or guidance on a more efficient approach to solving this problem.
let bakeryA = ['saffron', 'eggs', 'tomato paste', 'coconut', 'custard'];
let bakeryB = ['milk', 'butter', 'cream cheese'];
let recipes = [
{
name: 'Coconut Sponge Cake',
ingredients: ['coconut', 'cake base']
},
{
name: 'Persian Cheesecake',
ingredients: ['saffron', 'cream cheese']
},
{
name: 'Custard Surprise',
ingredients: ['custard', 'ground beef']
}
];
const chooseRecipe = function(bakeryA, bakeryB, recipes) {
let suitableRecipe = [];
for (let i = 0; i < recipes.length; i++) {
for (let j = 0; j < recipes[i].ingredients.length; j++) {
for (let k = 0; k < bakeryA.length; k++) {
if (bakeryA[k] === recipes[i].ingredients[j]) {
for (let l = 0; l < bakeryB.length; l++) {
for (let m = 0; m < recipes[i].ingredients; m++) {
if (bakeryB[l] === recipes[i].ingredients[m]) {
suitableRecipe.push(recipes[i].name);
}
}
}
}
}
}
}
return suitableRecipe;
}
console.log(chooseRecipe(bakeryA, bakeryB, recipes));