Created
May 30, 2014 19:23
-
-
Save federicobucchi/79ed65f455c063932c8f to your computer and use it in GitHub Desktop.
Fibonacci Sequence
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
//Normal | |
function fibonacci(number) { | |
if (number > -1) { | |
var arr = []; | |
for (var i=0; i<=number; i++){ | |
if (i==0){ | |
arr.push(0); | |
} else if (i==1){ | |
arr.push(1); | |
} else { | |
arr.push(arr[i-1] + arr[i-2]); | |
} | |
} | |
return(arr[number]); | |
} else { | |
return undefined; | |
} | |
} | |
fibonacci(5); | |
//CodeEval | |
var fs = require("fs"); | |
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) { | |
if (line !== "") { | |
fibonacci(line); | |
} | |
}); | |
function fibonacci(number) { | |
var numberCheck = parseInt(number, 10); | |
if (numberCheck > -1) { | |
var arr = []; | |
for (var i=0; i<=numberCheck; i++){ | |
if (i===0){ | |
arr.push(0); | |
} else if (i==1){ | |
arr.push(1); | |
} else { | |
arr.push(arr[i-1] + arr[i-2]); | |
} | |
} | |
return (arr[numberCheck]); | |
} else { | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment