Skip to content

Instantly share code, notes, and snippets.

@IrhaAli
Created October 8, 2022 17:07
Show Gist options
  • Save IrhaAli/c5287396e6fd99614c82831c3e6a41ce to your computer and use it in GitHub Desktop.
Save IrhaAli/c5287396e6fd99614c82831c3e6a41ce to your computer and use it in GitHub Desktop.
Given a list of recipes and list of ingredients from 2 bakeries, returns the recipe that uses an ingredient from each bakery
const chooseRecipe = function(bakeryA, bakeryB, recipes) {
let recipeFound = '';
//iterate through the recipes until one has been found
let i = 0;
while ((recipeFound === '') && (i < recipes.length)){
//iterate through bakeryA ingredients until a recipe has been found
let j = 0
while ((recipeFound === '') && (j < bakeryA.length)){
if ((bakeryA[j] === recipes[i].ingredients[0]) || (bakeryA[j] === recipes[i].ingredients[1])){
//iterate through bakeryA ingredients until a recipe has been found
let k = 0;
while ((recipeFound === '') && (k < bakeryA.length)){
if ((bakeryB[k] === recipes[i].ingredients[0]) || (bakeryB[k] === recipes[i].ingredients[1])){
recipeFound = recipes[i].name;
}
k++;
}
}
j++;
}
i++;
}
return recipeFound;
}
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']
}
];
console.log(chooseRecipe(bakeryA, bakeryB, recipes));
bakeryA = ['potatoes', 'bay leaf', 'raisins'];
bakeryB = ['red bean', 'dijon mustard', 'apples'];
recipes = [
{
name: 'Potato Ganache',
ingredients: ['potatoes', 'chocolate']
},
{
name: 'Sweet Fish',
ingredients: ['anchovies', 'honey']
},
{
name: "Nima's Famous Dijon Raisins",
ingredients: ['dijon mustard', 'raisins']
}
];
console.log(chooseRecipe(bakeryA, bakeryB, recipes));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment