0
JavaScript Challenge
function fib(num) { if(num <= 2) return 1; return fib(num-1)+fib(num-2); } document.write(fib(6)); //8 The output of this code is 8 but why?
1 Answer
+ 3
Romjan Ali
//fib(6)
//fib(5) + fib(4)
//(fib(4) + fib(3)) + (fib(3) + fib(2))
//(fib(3) + fib(2)) + (fib(2) + fib(1)) + (fib(2) + fib(1)) + 1
//(fib(2) + fib(1)) + 1 + 1 + 1 + 1 + 1 + 1
//1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
//8