The Sum of a Range
Range function:
function range(start, end, step) {
let array = [];
if (step != undefined) {
let i = start;
if(step > 0) {
while (i <= end) {
array.push(i);
i += step;
}
} else if (step < 0) {
while (i >= end) {
array.push(i)
i += step;
}
}
} else {
for (i = start; i <= end; i++) {
array.push(i);
}
}
return array;
}
Sum function:
function sum(num) {
let sum = 0;
let array = num;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
Reversing an Array
Produces new array:
function reverseArray(array) {
let newArray = [];
for (let i = 0; i < array.length; i++) {
let value = array[i];
newArray.unshift(value);
}
return newArray;
}
Modifies array:
function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}