Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d9f357f

Browse files
committed
Add a question
1 parent 49678a9 commit d9f357f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,3 +1560,46 @@ const generateHashtag = str => {
15601560
---
15611561

15621562
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1563+
1564+
## 47. Pete, the baker
1565+
1566+
Pete likes to bake some cakes. He has some recipes and ingredients. Unfortunately he is not good in maths. Can you help him to find out, how many cakes he could bake considering his recipes?
1567+
1568+
Write a function `cakes()`, which takes the `recipe` object and the `available` ingredients object and returns the maximum number of cakes Pete can bake. Ingredients that are not present in the objects, can be considered as `0`.
1569+
1570+
```js
1571+
const cakes = (recipe, available) => {
1572+
// Your solution
1573+
};
1574+
1575+
let recipe = { flour: 500, sugar: 200, eggs: 1 };
1576+
let available = { flour: 1200, sugar: 1200, eggs: 5, milk: 200 };
1577+
console.log(cakes(recipe, available)); // 2
1578+
1579+
recipe = { apples: 3, flour: 300, sugar: 150, milk: 100, oil: 100 };
1580+
available = { sugar: 500, flour: 2000, milk: 2000 };
1581+
console.log(cakes(recipe, available)); // 0
1582+
```
1583+
1584+
<details><summary>Solution</summary>
1585+
1586+
```js
1587+
const cakes = (recipe, available) => {
1588+
let num_cakes = Infinity;
1589+
for (let ingredient in recipe) {
1590+
if (!available[ingredient] || recipe[ingredient] > available[ingredient])
1591+
return 0;
1592+
num_cakes = Math.min(
1593+
num_cakes,
1594+
Math.floor(available[ingredient] / recipe[ingredient])
1595+
);
1596+
}
1597+
return num_cakes;
1598+
};
1599+
```
1600+
1601+
</details>
1602+
1603+
---
1604+
1605+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /