Created
September 13, 2014 04:05
-
-
Save manderly/793376143b987b57b1ca to your computer and use it in GitHub Desktop.
Highest Fibonacci number under maxNum (improved)
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
//Return to me the highest Fibonacci number under 100 | |
//0,1,1,2,3,5,8,13,21,34,55,89 | |
var maxNum = 100; | |
var sum = 0; | |
var findHighestFiboUnder = function(max, num1, num2) { | |
//allows num1 and num2 to be optional | |
num1 = typeof num1 !== 'undefined' ? num1: 1; | |
num2 = typeof num2 !== 'undefined' ? num2: 1; | |
sum = num1 + num2; | |
if (sum < max) { | |
num1 = num2; | |
num2 = sum; | |
return findHighestFiboUnder(max,num1,num2); | |
} else { | |
return num2; | |
} | |
}; | |
console.log("Highest Fibonacci number under " + maxNum + ": " + (findHighestFiboUnder(maxNum))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment