Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. // my solution in JavaScript for the 'Chocolate Feast' problem on HR.
  2. function chocolateFeast(n, c, m) {
  3. // determine how many bars are initially bought.
  4. const bought = Math.floor(n / c);
  5. console.log(`Bobby bought ${bought} with ${n} at ${c} each.`);
  6. // now, determine how many were bought with the promo.
  7. let promo = 0;
  8. let wrappers = bought;
  9. // since we get additional wrappers with the promo, we need a looping condition
  10. // to continue accumulating until we no longer have enough wrappers.
  11. while (wrappers >= m) {
  12. let given = Math.floor(wrappers / m);
  13. console.log(`Bobby has ${wrappers} wrappers, so he gets ${given} bonus bars.`);
  14. if (given > 0) {
  15. promo += given;
  16. // the wrappers value should be updated with the remainder plus the number
  17. // given as part of the promotion.
  18. wrappers = Math.floor(wrappers %m) + given;
  19. }
  20. }
  21. console.log(`Bobby gets ${bought + promo} total bars.`);
  22. return bought + promo;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement