Difference between recursion and iteration - JavaScript

Hi,

Can you please provide me a simple example on:

Difference between recursion and iteration - JavaScript

Thanks
Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
PluritechnicianCommented:
Test page (recursion) : http://jsfiddle.net/Mjzkk/
``````function factorial_recursion(n) {
if(n!=1) n * factorial_recursion;
}
``````

test page (iteration) : http://jsfiddle.net/Kvmrc/
``````function factorial_iteration(n) {
a = n;
while(n!=1) {
n = n -1;
a *= n;
}
return a;
}
``````

http://en.wikipedia.org/wiki/Factorial

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

PluritechnicianCommented:
correction (recursion) :
``````function factorial_recursion(n) {
if(n!=1) n = n * factorial_recursion(n-1);
return n;
}
``````
PluritechnicianCommented:
to be mathematicaly correct (and not just for the example) : 0! = 1
so :

``````function factorial_recursion(n) {
if(n==0) return 1;
if(n!=1) n * factorial_recursion(n-1);
return n;
}
``````
and :
``````function factorial_iteration(n) {
if(n==0) return 1;
a = n;
while(n!=1) {
n = n -1;
a *= n;
}
return a;
}
``````