...

/

Solution Review: Make Maximum Donuts

Solution Review: Make Maximum Donuts

This lesson will explain the solution to the problem in the previous lesson.

Solution #

Press + to interact
const maxDonuts = (recipe, quantity) =>
Math.floor(
Math.min(...Object.keys(recipe).map(k => quantity[k] / recipe[k] || 0))
)
const ans = maxDonuts({ flour: 2, sugar: 40, butter: 20 },{ flour: 5, sugar: 120, butter: 500 })
console.log(ans)

Explanation #

The maxDonuts function takes two objects as parameters; the recipe and the quantity of ingredients available.

To make the donuts, we need all the ingredients of the recipe given. In the above scenario, we need the following:

{ flour: 2, sugar: 40, butter: 20 }

To make a whole donut, we will need either the exact amount of ingredients mentioned in the recipe or greater. We can’t make a whole donut if the ...

Access this course and 1400+ top-rated courses and projects.