Skip to content

Instantly share code, notes, and snippets.

@wejrowski
Last active April 3, 2017 20:15
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 wejrowski/32b12b6a9830ce818ee470fc6b511e35 to your computer and use it in GitHub Desktop.
Save wejrowski/32b12b6a9830ce818ee470fc6b511e35 to your computer and use it in GitHub Desktop.
Quiz: next fibonacci number
var getNextFibonacciAfter = (function() {
var fibonacciNumbers = [0, 1];
function nextFibonnacciIn(numbers) {
return numbers[numbers.length] + numbers[numbers.length - 1];
}
return function(number) {
var nextFibonacci;
if (number <= 1) {
throw new Error("Your number must be greater than 1");
}
while (fibonacciNumbers[fibonacciNumbers.length-1] <= number) {
fibonacciNumbers.push(nextFibonnacciIn(fibonacciNumbers));
}
nextFibonacci = fibonacciNumbers[fibonacciNumbers.indexOf(number) + 1];
if (nextFibonacci) {
return nextFibonacci;
} else {
throw new Error(number + " is not a fibonacci number.");
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment