浏览器没有响应

时间:2015-01-28 06:28:53

标签: javascript arrays

我写了这段小代码来计算数字数组中的负数,零和正数的数量。当我运行代码时,浏览器不会响应,我必须杀死它!任何帮助将不胜感激。谢谢。

   <script type = "text/javascript">
var promptArrayNum = prompt("Enter an array of numbers (Separated by comma): ");
var arrayNum1 = promptArrayNum.toString();
var arrayNum2 = [arrayNum1];
var arrayNum3 = counter(arrayNum2);
function counter(number){
    var count;
    var countNeg = 0;
    var countPos = 0;
    var countZero = 0;
    for (var i = 0; i <= arrayNum2.length; i++)
    {
        switch (true){
            case (arrayNum2[i] < 0): countNeg++;
                document.write("Negative elements = " + countNeg);
                break;
            case (arrayNum2[i] = 0): countZero++;
                document.write("Zero elements = " + countZero);
                break;
            case (arrayNum2[i] > 0): countPos++;
                document.write("Positive elements = " + countPos);
                break;
            default: {document.write("Array is invalid");}          
        }
    }
    return count;
}   

5 个答案:

答案 0 :(得分:1)

首先,它不是您正在操作的数组。只需拆分,

即可
var promptArrayNum = prompt("Enter an array of numbers (Separated by comma): ");
var arr = promptArrayNum.split(",");

此外,您的代码可以更有效,如

var numOf0s = arr.filter(function(e){ return e == 0 }).length,
    numberOfPositives = arr.filter(function(e){ return e > 0 }).length,
    numberOfNegatives = arr.filter(function(e){ return e < 0 }).length;

答案 1 :(得分:1)

虽然您的算法不正确。无限循环的原因是行

case (arrayNum2[i] = 0): countZero++;

您已经使用了赋值运算符,并且您正在遍历arrayNum2。它的大小在每个循环中都在增加。

而应该是

case (arrayNum2[i] == 0): countZero++;

答案 2 :(得分:0)

这是代码的简化工作版本

  1. 使用split生成输入数组
  2. 使用if语句而不是切换
  3. arrayNum2 [i] = 0是赋值,而不是相等比较(使用===(严格相等)
  4. 在循环外移动警报
  5. http://jsfiddle.net/hk6uger4/2/

    var input = prompt("Enter an array of numbers (Separated by comma): ");
    
    var arr = input.split(',');
    
    var count;
    var countNeg = 0;
    var countPos = 0;
    var countZero = 0;
    var i = 0;
    
    for (i = 0; i <= arr.length; i++) {
    
        if (arr[i] < 0) {
            countNeg++;
        }
        if (arr[i] === 0) {
            countZero++;
        }
        if (arr[i] > 0) {
            countPos++;
        }
    }
    
    alert("Negative elements = " + countNeg);
    alert("Zero elements = " + countZero);
    alert("Positive elements = " + countPos);
    

答案 3 :(得分:0)

现在有效!我花了2个多小时来弄明白。我是JS的新手。

 <script type = "text/javascript">
var promptArrayNum = prompt("Enter an array of numbers (Separated by comma): ");
var arrayNum = promptArrayNum.toString();
var arrayData = [arrayNum];
arrayData = arrayNum.split(",");
var arrayImplement = counter(arrayData);
function counter(number){
    var count;
    var countNeg = 0;
    var countPos = 0;
    var countZero = 0;
    for (var i = 0; i < arrayData.length; i++){             
            switch (true){
            case (arrayData[i] < 0): countNeg++;                    
                break;
            case (arrayData[i] == 0): countZero++;                  
                break;
            case (arrayData[i] > 0): countPos++;                    
                break;
            default: {document.write("Array is invalid");}          
        }
    }
        document.write("The array of numbers entered is: " + arrayData, "<br/>");
        document.write("Negative elements = " + countNeg, "<br/>");
        document.write("Zero elements = " + countZero, "<br/>");
        document.write("Positive elements = " + countPos, "<br/>");
    return count;
}   

答案 4 :(得分:-1)

哦,奇怪的代码。试试这个:

var promptArrayNum = prompt("Enter an array of numbers (Separated by comma): ");
var data = promptArrayNum.split(",");
data = data.map(function(item){if(!item)return 0;else return parseInt(item.trim())});
for(var i = 0; i < data.length; i++){
var item = data[i];
if(item == 0){alert("item == 0")}
else if(item > 0) { alert("item > 0")}
else alert("item < 0")

}