Advertisement
Spocoman

06. Cake

Dec 28th, 2021 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЯ С WHILE:
  2.  
  3. function cake(input) {
  4.     let index = 2;
  5.     let width = Number(input[0]);
  6.     let length = Number(input[1]);
  7.     let cakePieces = width * length;
  8.     let command = input[index];
  9.  
  10.     while (command !== "STOP") {
  11.         cakePieces -= Number(command);
  12.         if (cakePieces <= 0) {
  13.             break;
  14.         }
  15.         index++;
  16.         command = input[index];
  17.     }
  18.     if (cakePieces >= 0) {
  19.         console.log(`${cakePieces} pieces are left.`);
  20.     } else {
  21.         console.log(`No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  22.     }
  23. }
  24.  
  25. Или леко тарикатската:)
  26.  
  27. function cake(input) {
  28.     let index = 2;
  29.     let cakePieces = Number(input[0]) * Number(input[1]);
  30.  
  31.     while (input[index] !== "STOP" && cakePieces > 0) {
  32.         cakePieces -= Number(input[index++]);
  33.     }
  34.  
  35.     if (cakePieces >= 0) {
  36.         console.log(`${cakePieces} pieces are left.`);
  37.     } else {
  38.         console.log(`No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  39.     }
  40. }
  41.  
  42. Решение със shift() и тернарен оператор:
  43.  
  44. function cake(input) {
  45.     let cakePieces = Number(input.shift()) * Number(input.shift());
  46.  
  47.     while (input[0] !== "STOP" && cakePieces > 0) {
  48.         cakePieces -= Number(input.shift());
  49.     }
  50.  
  51.     console.log(cakePieces >= 0 ? `${cakePieces} pieces are left.` :
  52.         `No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  53. }
  54.  
  55. РЕШЕНИЯ С FOR:
  56.  
  57. function cake(input) {
  58.     let width = Number(input[0]);
  59.     let length = Number(input[1]);
  60.     let cakePieces = width * length;
  61.  
  62.     for (let i = 2; i < input.length; i++) {
  63.         let command = input[i];
  64.         if (command === "STOP") {
  65.             break;
  66.         }
  67.         cakePieces -= Number(command);
  68.         if (cakePieces <= 0) {
  69.             break;
  70.         }
  71.     }
  72.  
  73.     if (cakePieces >= 0) {
  74.         console.log(`${cakePieces} pieces are left.`);
  75.     } else {
  76.         console.log(`No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  77.     }
  78. }
  79.  
  80. Или леко тарикатската и с тернарен оператор:)
  81.  
  82. function cake(input) {
  83.     let cakePieces = Number(input[0]) * Number(input[1]);
  84.  
  85.     for (let i = 2; i < input.length; i++) {
  86.         if (input[i] === "STOP" || cakePieces < 0) {
  87.             break;
  88.         }
  89.         cakePieces -= Number(input[i]);
  90.     }
  91.  
  92.     console.log(cakePieces >= 0 ? `${cakePieces} pieces are left.` :
  93.         `No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  94. }
  95.  
  96. РЕШЕНИЕ С МЕТОДИ И ТЕРНАРЕН ОПЕРАТОР:
  97.  
  98. function cake(input) {
  99.     let cakePieces = Number(input.shift()) * Number(input.shift()) -
  100.         input.slice(0, input.includes("STOP") ? -1 : input.length).map(Number).reduce((a, b) => a + b);
  101.  
  102.     console.log(cakePieces >= 0 ? `${cakePieces} pieces are left.` :
  103.         `No more cake left! You need ${Math.abs(cakePieces)} pieces more.`);
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement