Didart

Cake

Apr 19th, 2022
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cake(input) {
  2.     let index = 0;
  3.  
  4.     let width = Number(input[index]);
  5.     index++;
  6.     let length = Number(input[index]);
  7.     index++;
  8.  
  9.     let cakeSize = width * length;
  10.  
  11.     let command = input[index];
  12.     index++;
  13.  
  14.     let isCake = true;
  15.     while (command !== "STOP") {
  16.         let pieces = Number(command);
  17.         cakeSize -= pieces;
  18.  
  19.         if (cakeSize < 0) {
  20.             isCake = false;
  21.             break;
  22.         }
  23.         command = input[index];
  24.         index++;
  25.     }
  26.  
  27.     if (isCake) {
  28.         console.log(`${cakeSize} pieces are left.`);
  29.     } else {
  30.         console.log(`No more cake left! You need ${Math.abs(cakeSize)} pieces more.`);
  31.     }
  32. }
  33.  
  34. cake(["10", "10", "20", "20", "20", "20", "21"])
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment