In VBScript, the Ashes levels are calculated from 1 to 100 and _vbs.

Source: Internet
Author: User
This log is a complete reading of a journal inspired by the original: Evil eval and New Function.

We rarely use the new array to define an array, and I don't think that the new array has such a magical magic that we can create n empty elements with the new array (n), and there are n-1 blanks in the middle of n elements, and by join, We can put something in these blanks to make up the special strings that we need, which inspires me that I can easily implement a "continuous operation of regular elements" without a loop. For example, to calculate the number of 1-100. In the past we needed to loop from 1 to 100 and then sum, but with this feature of the array we could do it with a simple line of code, as follows:
Copy Code code as follows:

var i=0,sum=eval (' 0 ' +new Array (). Join (' + (++i) '));

In addition, you can do many similar mathematical operations, such as the following
Copy Code code as follows:

Eval (' 0 ' +new Array (one). Join (' +math.pow (++i,2) ')//compute 1-10 squared sum
Eval (' 0 ' +new Array (100/2). Join (' + (i+=2) ')//COMPUTE 2+4+6+ ... 100 of and

These we have in the past through the loop of the calculation, now can be a line of code cleverly done, have to admire the implicit programming language dexterity, programming the world there are always too many things waiting for us to find.

To change the above JavaScript code to a VBS, is the so-called "Ashes-level solution" of the VBS exercises:

Copy Code code as follows:

Dim A (101)
Execute (Join (A, "s=s+i:i=i+1:"))
MsgBox s


Original: http://demon.tw/programming/no-loop-calculate-1-100-sum.html

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.