Skip to content

Instantly share code, notes, and snippets.

@harryadel
Created August 27, 2016 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryadel/22686b5e5c54056081b76061bcd18819 to your computer and use it in GitHub Desktop.
Save harryadel/22686b5e5c54056081b76061bcd18819 to your computer and use it in GitHub Desktop.
/*
My answer for exercise 'The sum of a range'
in Eloquent JavaScript Second Edition
Chapter 4 Data Structures: Objects and Arrays
*/
function range(first, second, third) {
var array = [];
if (first < second) {
while (first <= second ) {
array.push(first++);
}
} else {
while (first >= second) {
array.push(first--);
}
}
return array;
}
function sum(array) {
var output = 0;
for(var i = 0; i < array.length; i++) {
output += array[i];
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment