Skip to main content
🧩 Algorithms & Problem Solving

Fibonacci

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13… Each term is the sum of the two before it. An iterative approach is far faster than naive recursion.

let a = 0, b = 1;
// roll the pair forward n times

Your task

Write fib(n) that returns the n-th Fibonacci number (0-indexed: fib(0) is 0, fib(1) is 1).

⚑ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
fib(0) === 0
fib(1) === 1
fib(7) === 13
fib(10) === 55

Run your code to check these.