Advertisement
Spocoman

03. Sum Numbers

Dec 26th, 2021 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function SumNumbers(input) {
  2.     let firstNum = Number(input[0]);
  3.     let index = 1;
  4.     let sum = 0;
  5.  
  6.     while (firstNum > sum) {
  7.         sum += Number(input[index]);
  8.         index++;
  9.     }
  10.     console.log(`${sum}`);
  11. }
  12.  
  13. ИЛИ ТАКА:
  14.  
  15. function SumNumbers(input) {
  16.     let firstNum = Number(input[0]);
  17.     let index = 0;
  18.     let sum = 0;
  19.  
  20.     while (firstNum > sum) {
  21.         sum += Number(input[++index]);
  22.     }
  23.     console.log(`${sum}`);
  24. }
  25.  
  26. РЕШЕНИЕ СЪС SHIFT:
  27.  
  28. function SumNumbers(input) {
  29.     let firstNum = Number(input.shift());
  30.     let sum = 0;
  31.  
  32.     while (firstNum > sum) {
  33.         sum += Number(input.shift());
  34.     }
  35.     console.log(`${sum}`);
  36. }
  37.  
  38. РEШЕНИЕ С FOR:
  39.  
  40. function sumNumbers(input) {
  41.     let firstNum = Number(input[0]);
  42.     let sum = 0;
  43.  
  44.     for (let i = 1; i <= Number.MAX_VALUE; i++) {
  45.         sum += Number(input[i]);
  46.         if (firstNum <= sum) {
  47.             console.log(`${sum}`);
  48.             return;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement