MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / range

Function range

code/solutions/04_1_the_sum_of_a_range.js:1–10  ·  view source on GitHub ↗
(start, end, step = start < end ? 1 : -1)

Source from the content-addressed store, hash-verified

1function range(start, end, step = start < end ? 1 : -1) {
2 let array = [];
3
4 if (step > 0) {
5 for (let i = start; i <= end; i += step) array.push(i);
6 } else {
7 for (let i = start; i >= end; i += step) array.push(i);
8 }
9 return array;
10}
11
12function sum(array) {
13 let total = 0;

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected