answersLogoWhite

0

I did this as an answre to some common js questions , the question was

Write a function which will return you first two times 1, then 2, then 3, then 5 and so on (Fibonacci numbers). Don't use any global variables.

var fibonacci = (function () {

var arr = [0, 1];

return function () {

var num = arr[arr.length - 1],

len = arr.length;

arr.push(arr[len - 1] + arr[len - 2]);

return num;

};

}());

//test

var i;

for (i = 0; i < 10; i++) {

console.log(fibonacci());

}

//1,1,2,3,5,8,13,21,34,55

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
BeauBeau
You're doing better than you think!
Chat with Beau

Add your answer:

Earn +20 pts
Q: Write a javascript program for Fibonacci series?
Write your answer...
Submit
Still have questions?
magnify glass
imp