-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbakeoff1.js
More file actions
95 lines (68 loc) · 2.24 KB
/
bakeoff1.js
File metadata and controls
95 lines (68 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// chooseRecipe returns the recipe that has one correct ingredient in the first bakery and the other ingredient in the other bakery
const chooseRecipe = function(bakeryA, bakeryB, recipes) {
// The ingredientIndex function returns the index of the ingredient that is supplied by the given bakery
const ingredientIndex = function (bakery, ingredients) {
let ingredientsIndex;
for (let ingredient of ingredients) {
ingredientsIndex = bakery.indexOf(ingredient);
if (ingredientsIndex !== -1) {
return ingredientsIndex;
}
}
return -1;
}
let ingredientInA; // Index of the ingredient that is in bakery A;
let ingredientInB;
let recipeIngredients;
// If one of the ingredients is in A, the program splices it out of the recipeIngredients array
// so that it only searches the remaining ingredient in Bakery B.
for (let recipe of recipes) {
recipeIngredients = recipe.ingredients;
ingredientInA = ingredientIndex(bakeryA, recipeIngredients);
if (ingredientInA === -1) {
continue;
}
recipeIngredients.splice(ingredientInA, 1);
ingredientInB = ingredientIndex(bakeryB, recipeIngredients);
if (ingredientInB === -1) {
continue;
}
return recipe.name;
}
console.log("Valid Recipe not found");
return false;
}
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));