Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // my solution in JavaScript for the 'Chocolate Feast' problem on HR.
- function chocolateFeast(n, c, m) {
- // determine how many bars are initially bought.
- const bought = Math.floor(n / c);
- console.log(`Bobby bought ${bought} with ${n} at ${c} each.`);
- // now, determine how many were bought with the promo.
- let promo = 0;
- let wrappers = bought;
- // since we get additional wrappers with the promo, we need a looping condition
- // to continue accumulating until we no longer have enough wrappers.
- while (wrappers >= m) {
- let given = Math.floor(wrappers / m);
- console.log(`Bobby has ${wrappers} wrappers, so he gets ${given} bonus bars.`);
- if (given > 0) {
- promo += given;
- // the wrappers value should be updated with the remainder plus the number
- // given as part of the promotion.
- wrappers = Math.floor(wrappers %m) + given;
- }
- }
- console.log(`Bobby gets ${bought + promo} total bars.`);
- return bought + promo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement