Created
June 23, 2015 00:48
-
-
Save rhlowe/4314b567f3cc731d4f2a to your computer and use it in GitHub Desktop.
sum, range, and fibonacci
This file contains 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
function range(start, end) { | |
var output = []; | |
for(var i = start; i <= end; i++) { | |
output.push(i); | |
} | |
return output; | |
} | |
// console.log(range(1,10)); | |
function sum(array) { | |
var output = 0; | |
for(var i = 0; i < array.length; i++) { | |
output += array[i]; | |
} | |
return output; | |
} | |
// console.log(sum([1,2,3,4,5])); | |
function fibonacci(length) { | |
var output = [0,1]; | |
for (var i = 2; i < length; i++) { | |
var last = output[output.length-1]; | |
var nextToLast = output[output.length-2]; | |
output.push(last + nextToLast); | |
} | |
return output; | |
} | |
// console.log(fibonacci(2)); // [0,1] | |
// console.log(fibonacci(5)); // [0,1,1,2,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment