-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batches.js
56 lines (49 loc) · 1.26 KB
/
batches.js
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
/**
It accepts two objects as arguments: the first object is the recipe
for the food, while the second object is the available ingredients.
Each ingredient's value is a number representing how many units there are.
`batches(recipe, available)`
*/
function batches(recipe, available) {
const recipeArray = Object.keys(recipe);
const possibleServings = [];
for (const ingredient of recipeArray) {
possibleServings.push(available[ingredient] / recipe[ingredient] || 0);
}
return Math.floor(Math.min(...possibleServings));
}
// const batches = (recipe, available) =>
// Math.floor(
// Math.min(
// ...Object.keys(recipe).map(
// ingredient => available[ingredient] / recipe[ingredient] || 0
// )
// )
// );
// 0 batches can be made
console.log(
batches(
{ milk: 100, butter: 50, flour: 5 },
{ milk: 132, butter: 48, flour: 51 }
)
);
console.log(
batches(
{ milk: 100, flour: 4, sugar: 10, butter: 5 },
{ milk: 1288, flour: 9, sugar: 95 }
)
);
// 1 batch can be made
console.log(
batches(
{ milk: 100, butter: 50, cheese: 10 },
{ milk: 198, butter: 52, cheese: 10 }
)
);
// 2 batches can be made
console.log(
batches(
{ milk: 2, sugar: 40, butter: 20 },
{ milk: 5, sugar: 120, butter: 500 }
)
);