I was fortunate enough to get a small interview question recently. I studied it and then shared it. I hope it will be helpful to everyone's coding career.
The interview question is: Find all numbers from 1-1000 that appear 1, and count the number of 1 occurrences
Solution:
//The enumeration method can only be viewed under 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 it does not work if you go from 1-N, because N may not be how many.