Created
October 26, 2021 18:35
-
-
Save Elijah-trillionz/ba80aefcc9c96bad3aeedd4d49808e31 to your computer and use it in GitHub Desktop.
Just like python range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(start, end, step = 1) { | |
const rangeOfNums = [] | |
if (step < 0) { | |
for (let i = start; i >= end; i -= Math.abs(step)) { | |
rangeOfNums.push(i); | |
}; | |
} else { | |
for (let i = start; i <= end; i += step) { | |
rangeOfNums.push(i); | |
}; | |
} | |
return rangeOfNums; | |
} | |
function sum(nums) { | |
let sumOfNums = 0 | |
for (let i = 0; i <nums.length; i++) { | |
sumOfNums += nums[i] | |
} | |
return sumOfNums; | |
} | |
console.log(sum(range(5, 2, -1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment