-
-
Save jonurry/7281f3e527a8c79495ebf1b8bec8edc2 to your computer and use it in GitHub Desktop.
// step parameter is optional | |
// if step is not passed in, | |
// and start is less than or equal to end, | |
// then step = 1, else step = -1 | |
function range(start, end, step = start <= end ? 1 : -1) { | |
let result = []; | |
// loop iterates up for positive step values | |
// and iterates down for negative step values | |
for (let i = start; step >= 0 ? i <= end : i >= end; i+=step) { | |
result.push(i); | |
} | |
return result; | |
} | |
function sum(numbers) { | |
result = 0; | |
for (let num of numbers) { | |
result += num; | |
} | |
return result; | |
} | |
console.log(range(1, 10)); | |
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
console.log(range(1, 10, 2)); | |
// → [1, 3, 5, 7, 9] | |
console.log(range(5, 2, -1)); | |
// → [5, 4, 3, 2] | |
console.log(range(5, 2)); | |
// → [5, 4, 3, 2] | |
console.log(range(10, 1, -3)); | |
// → [10, 7, 4, 1] | |
console.log(sum(range(1, 10))); | |
// → 55 | |
console.log(sum(range(1, 10, 2))); | |
// → 25 | |
console.log(sum(range(10, 1, -3))); | |
// → 22 (10 + 7 + 4 + 1) |
Hints
Building up an array is most easily done by first initializing a binding to [] (a fresh, empty array) and repeatedly calling its push
method to add a value. Don’t forget to return the array at the end of the function.
Since the end boundary is inclusive, you’ll need to use the <= operator rather than < to check for the end of your loop.
The step
parameter can be an optional parameter that defaults (using the = operator) to 1.
Having range
understand negative step
values is probably best done by writing two separate loops—one for counting up and one for counting down—because the comparison that checks whether the loop is finished needs to be >= rather than <= when counting downward.
It might also be worthwhile to use a different default step
, namely, -1, when the end of the range is smaller than the start. That way, range(5, 2)
returns something meaningful, rather than getting stuck in an infinite loop. It is possible to refer to previous parameters in the default value of a parameter.
Thank you so so so much indeed really helpful
especially with explain.
4. Data Structures: Objects and Arrays
4.1 The sum of a range
The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers:
console.log(sum(range(1, 10)));
Write a
range
function that takes two arguments,start
andend
, and returns an array containing all the numbers fromstart
, up to (and including)end
.Next, write a
sum
function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.As a bonus assignment, modify your
range
function to take an optional third argument that indicates the “step” value used to build up the array. If no step is given, the array elements go up by increments of one, corresponding to the old behaviour.The function call
range(1, 10, 2)
should return[1, 3, 5, 7, 9]
.Make sure it also works with negative step values so that
range(5, 2, -1)
produces[5, 4, 3, 2]
.