Last active
April 3, 2017 20:15
-
-
Save wejrowski/32b12b6a9830ce818ee470fc6b511e35 to your computer and use it in GitHub Desktop.
Quiz: next fibonacci number
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
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