(function(){
var a = 100;
for(a;a<1000;a++){
splitting(a);//调用函数是为了避免闭包
}
})();
function splitting(a){
var first = 0,second = 0,three = 0;
var newString = a.toString();
var allChart = [];
var i = 0;
while(newString.substr(i,1)!=""){//拆分数字,将个十百位拆成字符复制给数组
allChart[i]=newString.substr(i,1);
i++;
}
first = allChart[0];
second = allChart[1];
three = allChart[2];
if((first*first*first + second*second*second + three*three*three) == a){
console.log(a);
}
}
如果仍然想使用闭包,可以这么写
(function(){
var a = 100;
for(a;a<1000;a++){
(function(a){
var first = 0,second = 0,three = 0;
var newString = a.toString();
var allChart = [];
var i = 0;
while(newString.substr(i,1)!=""){
allChart[i]=newString.substr(i,1);
i++;
}
first = allChart[0];
second = allChart[1];
three = allChart[2];
if((first*first*first + second*second*second + three*three*three) == a){
console.log(a);
}
})(a);
}
})();