Evaluate the implementation of enumeration to obtain all numbers with 1 in 1-and calculate the number with 1 in 1-javascript

Source: Internet
Author: User
Obtain all the numbers with 1 in 1-and calculate the number of 1 in 1. The following is an enumeration method, but if 1-N is used, because N may not necessarily be the number of recent lucky ones to get a small interview question, study it, and then share it, hoping to help everyone's code career.

The following is an interview question: calculate the number of 1 in 1-and calculate the number of 1 in 1.

Solution:

The Code is as follows:


// The enumeration method can only be viewed in Google Chrome
(Function (){
Var tmp = [];
For (var I = 1; I <1001; ++ I ){
/1/g. test (''+ i-1) & tmp. push (I );
}
Console. log (tmp. length)

Console. log (tmp. reduce (function (I, j ){
Return I + j
}))

})()


The above is the implementation method of enumeration, but if it is 1-N, it will not work, because N may not necessarily be much.

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.