Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created August 27, 2014 23:28
Show Gist options
  • Save jikeytang/5dc420cefb948c9ad244 to your computer and use it in GitHub Desktop.
Save jikeytang/5dc420cefb948c9ad244 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140828-题目1
有个函数,他有2个参数a和b, 实现主要的功能是计算出1在a和b之间出现的次数,
比如:a=1024,b=1032,那么a和b之间的数就是:
1024 1025 1026 1027 1028 1029 1030 1031 1032
则有10个1出现在这些数中,那么函数返回的值就是10。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@wsgouwan
Copy link

function show(a, b, num){
            var arr = new Array(b-a+1);
//              arr.forEach(function( v, i , a ){ a[i] = i ;});
            for( var i= 0; i < b-a+1; i ++ ){
                arr[i] = a + i ;
            }
            console.log(arr.join('').split('').filter(function(v){ return v == 2}).length)
        };
        show(2001, 2092, 2 )

@LZ0211
Copy link

LZ0211 commented May 15, 2015

function countOneBetween(n,m){
    var counter = 0,
        number;
    for (var i=n;i<=m ;i++ ){
        number = i;
        do{
            if (number%10==1){
                counter++
            }
            number = parseInt(number/10);
        }
        while (number>0);
    }
    return counter;
}
console.log(countOneBetween(1024,1032))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment