@KK998/

Fibonacchi

Nodejs

fibonachi index number

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//given a number N return the index value of the Fibonacci squence, where the sequence is:
// 0,1,1,2,3,5,8,13,21,34,55,89,144 ...

function fibonacci(num) {
  let a = 1, b = 0, temp;

  while (num >= 0) {
    temp = a;
    a = a + b;
    b = temp;
    num--;
  }

  return b;
}

fibonacci(5);
/* 
function recursiveFibonacci(num){

  if( num < 2){
    return num;
  }
  return recursiveFibonacci(num-1) + recursiveFibonacci(num-2)
}
recursiveFibonacci(8) */
node v10.16.0