Skip to content

Instantly share code, notes, and snippets.

@alaztetik
Created April 14, 2020 08:32
Show Gist options
  • Save alaztetik/48732327d7bc48977b6acca6559353ec to your computer and use it in GitHub Desktop.
Save alaztetik/48732327d7bc48977b6acca6559353ec to your computer and use it in GitHub Desktop.
sum(range(start, end))
function range(num1, num2) {
const array = [];
if (typeof num1 !== 'number' || typeof num2 !== 'number') {
throw new Error('Please provide 2 numbers.');
}
for (let i = num1; i < num2; i++) {
array.push(i);
}
return array;
}
function sum(array) {
if (typeof array !== 'object') {
throw new Error('Please provide an array.');
}
let total = 0;
for (let i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment