Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Difference between recursion and iteration - JavaScript

Posted on 2011-09-11
7
323 Views
Last Modified: 2012-06-27
Hi,

Can you please provide me a simple example on:

Difference between recursion and iteration - JavaScript

Thanks
0
Comment
Question by:milani_lucie
7 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 125 total points
ID: 36520714
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 125 total points
ID: 36520738
Test page (recursion) : http://jsfiddle.net/Mjzkk/
function factorial_recursion(n) {
      if(n!=1) n * factorial_recursion;
}

Open in new window


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

Open in new window


http://en.wikipedia.org/wiki/Factorial
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36520739
correction (recursion) :
function factorial_recursion(n) {
    if(n!=1) n = n * factorial_recursion(n-1);
    return n;
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 82

Expert Comment

by:leakim971
ID: 36520742
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;
}

Open in new window

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

Open in new window

0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 125 total points
ID: 36520837
Recursion repeatedly invokes the mechanism itself as explained by @leakim971 using factorial number. Both iteration and recursion are based on a control structure. Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated method calls.

Keep in Mind:

Recursion consequently the overhead of method calls. This can be expensive in both processor time and memory space.
0
 
LVL 9

Expert Comment

by:user_n
ID: 36524764
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 125 total points
ID: 36524822
Recursion invokes the function itself. If you do not stop the process right it might proceed forever. It takes more space in the stack of the computer (http://en.wikipedia.org/wiki/Call_stack), because of witch it usually takes more time and memory.But in some cases it is the easiest way of finding of programing solution to a problem. Some more info (http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question