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?

21st Jul 2022, 2:48 AM
Romjan Ali
Romjan Ali - avatar
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
21st Jul 2022, 2:55 AM
AÍąJ
AÍąJ - avatar