Ivankooo1

Cake

Jun 13th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. function cake(input) {
  2. let l = Number(input.shift());
  3. let w = Number(input.shift());
  4.  
  5. let cake = l * w;
  6.  
  7. let command = input.shift();
  8.  
  9. let cakeSlise = 0;
  10. let counter = 0;
  11.  
  12. while (cakeSlise <= cake) {
  13.  
  14. if (command === 'STOP') {
  15. break;
  16. }
  17. let takeSlise = Number(command);
  18.  
  19.  
  20. cakeSlise += takeSlise;
  21. counter++;
  22. command = input.shift();
  23. }
  24. if (cakeSlise < cake) {
  25. let leftSlice = Math.abs(cake - cakeSlise);
  26. console.log(`${leftSlice} pieces are left.`);
  27. }
  28. else if (cakeSlise >= cake) {
  29. let left = Math.abs(cakeSlise - cake);
  30. console.log(`No more cake left! You need ${left} pieces more.`);
  31. }
  32.  
  33. }
Add Comment
Please, Sign In to add comment