javascript example
Fibonacci sequence in JavaScript
Print the first Fibonacci numbers with an iterative loop — no recursion, no install.
Keep the last two values and add them. Iteration stays fast and will not blow the stack on a longer list.
Change how many terms you print. If a run feels slow, shrink the count — the sandbox has a time limit.
function fibonacci(count) {
const values = [];
let a = 0;
let b = 1;
for (let i = 0; i < count; i++) {
values.push(a);
const next = a + b;
a = b;
b = next;
}
return values;
}
console.log(fibonacci(10).join(", "));