Javascript“ For”循环调试BEGINNER

我目前正在家里学习JavaScript,很乐意帮助您完成这项工作。我相信for循环是问题,但我不明白。这只是使我的浏览器冻结。

var formatNumber = function(num, type) {

   var numSplit, int, dec;

   num = Math.abs(num);
   num = num.toFixed(2); 
   numSplit =  num.split('.');
   int = numSplit[0];

   **if (int.length > 3) {
    for (var i = 3; i < int.length; i + 4) {
        int = int.substr(0,int.length - i) + ',' + int.substr(int.length - i, int.length);
        }
    };** 


   dec = numSplit[1];


   return (type === 'exp' ? sign = '-' : sign = '+') + '' + int + dec;

};
评论
  • det
    det 回复

    Your for loop will not increment and therefor i will always be < int.length. Use an addition assignment += to increase the value of i with 4.

    for (var i = 3; i < int.length; i += 4) {
      int = int.substr(0,int.length - i) + ',' + int.substr(int.length - i, int.length);
    }