Find Index of given fibonacci number in constant time | A simple Javascript program to find index of given Fibonacci number . ; If Fibonacci number is less than 2 , its index will be same as number ; Iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver code
function findIndex ( n ) { if ( n <= 1 ) return n ; let a = 0 , b = 1 , c = 1 ; let res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } let result = findIndex ( 21 ) ; document . write ( result ) ;