DEV Community

Discussion on: Fibonacci in Every Language

Collapse
 
_magedsaeed_ profile image
MagedSaeed • Edited

This is my first post here. I am intrigued by such kinds of problems. this is a simple enough JS code, I hope, to achieve the goal.
It is a recursive function, hence I am not expecting very match in terms of performance.

function fib(i){ 
    if (i<=0) return -1;
    else if (i==1) return 1;
    else if (i==2) return 1;
    else return fib(i-1)+fib(i-2)
}

// some examples
//fib(3) returns 2
//fib(5) returns 5

You may try this on chrome dev tools for quick tests.