Created
August 27, 2016 08:01
-
-
Save harryadel/22686b5e5c54056081b76061bcd18819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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