Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function SumNumbers(input) {
- let firstNum = Number(input[0]);
- let index = 1;
- let sum = 0;
- while (firstNum > sum) {
- sum += Number(input[index]);
- index++;
- }
- console.log(`${sum}`);
- }
- ИЛИ ТАКА:
- function SumNumbers(input) {
- let firstNum = Number(input[0]);
- let index = 0;
- let sum = 0;
- while (firstNum > sum) {
- sum += Number(input[++index]);
- }
- console.log(`${sum}`);
- }
- РЕШЕНИЕ СЪС SHIFT:
- function SumNumbers(input) {
- let firstNum = Number(input.shift());
- let sum = 0;
- while (firstNum > sum) {
- sum += Number(input.shift());
- }
- console.log(`${sum}`);
- }
- РEШЕНИЕ С FOR:
- function sumNumbers(input) {
- let firstNum = Number(input[0]);
- let sum = 0;
- for (let i = 1; i <= Number.MAX_VALUE; i++) {
- sum += Number(input[i]);
- if (firstNum <= sum) {
- console.log(`${sum}`);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement