Advertisement
Spocoman

Care of Puppy

Dec 11th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function careOfPuppy(input) {
  2.     let food = Number(input[0]) * 1000;
  3.     let i = 1;
  4.  
  5.     let command = input[i++]
  6.     while (command !== "Adopted") {
  7.         food -= Number(command);
  8.         command = input[i++];
  9.     }
  10.  
  11.     if (food >= 0) {
  12.         console.log(`Food is enough! Leftovers: ${food} grams.`)
  13.     } else {
  14.         console.log(`Food is not enough. You need ${Math.abs(food)} grams more.`);
  15.     }
  16. }
  17.  
  18. РЕШЕНИЕ С FOR:
  19.  
  20. function careOfPuppy(input) {
  21.     let food = Number(input[0]) * 1000;
  22.  
  23.     let command;
  24.     for (let i = 1; i < input.length; i++) {
  25.         command = input[i];
  26.         if (command === "Adopted") {
  27.             break;
  28.         }
  29.         food -= Number(command);
  30.     }
  31.  
  32.     if (food >= 0) {
  33.         console.log(`Food is enough! Leftovers: ${food} grams.`)
  34.     } else {
  35.         console.log(`Food is not enough. You need ${Math.abs(food)} grams more.`);
  36.     }
  37. }
  38.  
  39. РЕШЕНИЕ СЪС SHIFT() И ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:)
  40.  
  41. function careOfPuppy(input) {
  42.     let food = Number(input.shift()) * 1000;
  43.    
  44.     while (input[0] !== "Adopted") {
  45.         food -= Number(input.shift());
  46.     }
  47.  
  48.     console.log(food >= 0 ? `Food is enough! Leftovers: ${food} grams.`
  49.         : `Food is not enough. You need ${Math.abs(food)} grams more.`);
  50. }
  51.  
  52. РЕШЕНИЕ БЕЗ ЦИКЪЛ:
  53.  
  54. function careOfPuppy(input) {
  55.     let food = Number(input.shift()) * 1000 - input.slice(0, -1).map(Number).reduce((a, b) => a + b, 0);
  56.  
  57.     console.log(food >= 0 ? `Food is enough! Leftovers: ${food} grams.` : `Food is not enough. You need ${Math.abs(food)} grams more.`);
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement