_javascript techniques of recursive programming in JavaScript language

Source: Internet
Author: User
Title: From 1 Add up to 100 and how much?

Non-recursive loop writing:
Copy Code code as follows:

1run:function () {
2 var sum = 0;
3 for (Var i=1;i<=100;i++) {
4 sum = sum + i;
5}
6 console.log (sum);
7}

The recursive formulation:

Copy Code code as follows:

var testCase = {
sum:0,
Run:function (n) {
if (n>=100) {
return 100;
}
else {
sum = n+ Testcase.run (n+1);
return sum;
}
}
};
Console.log (Testcase.run (1));

The above code on the Internet a lot of search, the following is the equivalent of the wording:
Copy Code code as follows:

Console.log (function (n) {
var sum=0;
if (n<=1) {
return 1;
}
else{
sum = Arguments.callee (n-1) +n;
return sum;
}
}) (100));

This kind of writing is easy to learn. The above is linear recursion, as a recursive introduction of the line, the algorithm's performance efficiency is rotten, not to consider.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.