ProdanTenev

Cake

Mar 2nd, 2022
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 0.58 KB | None | 0 0
  1. function cake(input) {
  2.     index = 0;
  3.     let width = Number(input[index]);
  4.     index++;
  5.     let length = Number(input[index]);
  6.     index++;
  7.     let cakePieces = width * length;
  8.     while (cakePieces > 0) {
  9.         let command = input[index];
  10.         index++;
  11.         if (command === "STOP") {
  12.             console.log(`${cakePieces} pieces are left.`);
  13.             break;
  14.         }
  15.         cakePieces -= Number([command]);
  16.  
  17.     }
  18.     if (cakePieces <= 0) {
  19.         let diff = Math.abs(cakePieces);
  20.         console.log(`No more cake left! You need ${diff} pieces more.`);
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment