The implementation of the enumeration obtains 1-1000 of all occurrences of 1 digits and calculates the number of 1 _javascript tips

Source: Internet
Author: User
Recently had the honor to get a small interview question, studied the research, then shares out, hoped can be helpful to everybody's code cause.

The interview question is: Get 1-1000 of all occurrences of 1, and calculate the number of 1

Solution:
Copy Code code as follows:

Enumeration method can only be viewed under Google Browser
(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 of the enumeration, but if you do not use it from the 1-n, because n is not necessarily how 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.