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

What else can I help you with?

Related Questions

Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


How do you write a c program to calculate the Fibonacci series of a given number?

To write a C program that calculates the Fibonacci series up to a given number, you can use a loop to generate the series. Start by initializing the first two Fibonacci numbers (0 and 1) and then repeatedly compute the next number by adding the two preceding numbers until you reach or exceed the specified limit. Here’s a simple example: #include &lt;stdio.h&gt; int main() { int n, t1 = 0, t2 = 1, nextTerm; printf(&quot;Enter a positive integer: &quot;); scanf(&quot;%d&quot;, &amp;n); printf(&quot;Fibonacci Series: %d, %d&quot;, t1, t2); for (int i = 3; i &lt;= n; ++i) { nextTerm = t1 + t2; printf(&quot;, %d&quot;, nextTerm); t1 = t2; t2 = nextTerm; } return 0; } This program prompts the user for a number and prints the Fibonacci series up to that number.


Write a c program to generate Fibonacci series using copy constructor?

#include #include void main(void) { int i,j,k,n; clrscr(); i=0; j=1; printf("%d %d ",i,j); for(n=0;n&lt;=5;n++) { k=i+j; i=j; j=k; printf("%d ",k); } getch(); }


Write a javascript program out of three no which one s greater?

1 2 3


Write a program using while loop that generates Fabonaci series up to 200?

//to generate Fibonacci series upto a range of 200....(in C).... #include&lt;stdio.h&gt; main() { int a,b,c,i; a=0; b=1; printf("\n FIBONACCI SERIES .....\t"); i=1; while(i&lt;=(200-2)) { c=a+b; printf("\t%d",c); a=b; b=c; i++; } }


Write a java program to print the last digit in Fibonacci series?

Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.


How do you write a program that outputs a given characters in reverse?

write the javascript code to display the reverse no. of given no. (e.g. 247 reverse of 742)


Write a JavaScript program showing string concatenation?

There isn't much to it:---alert("Hello " + "world!");---'alert' creates a popup, and the + does string concatenation.See related link for a Javascript tutorial.


What is the difference between Javascript and Jquery?

JavaScript is a scripting language that is mainly used within a web browser. Jquery is a library of javascript code that can be used together with your javascript program. A set of functions that can preform common tasks is provided, which will reduce the amount of code that you have to write yourself.


Write a JavaScript program to print first ten odd natural numbers?

&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function series() { var i=1 while(i&lt;=10) { document.write(i+"&lt;br&gt;") i=i+2 } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="#" name=f1&gt; &lt;input type=button value="generate series" onclick="series()"&gt; &lt;/form&gt; &lt;br&gt;&lt;span id="outtab"&gt;&lt;/span&gt; &lt;/body&gt; &lt;/html&gt;


How do you write a program to print Fibonacci series of n numbers using recursion in cpp?

#include&lt;iostream&gt; unsigned fib (unsigned term, unsigned a=0, unsigned b=1) { if (term&lt;1) return a; return fib (--term, a+b, a); } int main() { std::cout &lt;&lt; "Fibonacci (1000th term): " &lt;&lt; fib (1000) &lt;&lt; std::endl; }


Write a C program to print the following series 112 122 . 1n2?

write a program to print the series 1/12+1/22+.........+1/n2 ?