js页面从.length冻结?

时间:2017-09-30 00:40:54

标签: javascript html

此功能冻结了我的页面。

function findMode (array)
{
    var modeArr = [];
    var modeCounter = [];
    modeArr.length = array.length;
    modeCounter.length = array.length;
}

但是,当我删除它时它运行得很好。

        modeArr.length = array.length;
        modeCounter.length = array.length;

以下是我的所有代码:

<html>
<head>

</head>
<body>

<p> Please enter a series of numbers, each separated by a new line.<br><p>

<textarea id="myTextArea" rows = "7" cols = "50"></textarea><br>

<button onclick="processData()">Done</button>

<p id = "mean"></p>
<p id = "median"></p>
<p id = "count"></p>
<p id = "summation"></p>
<p id = "mode"></p>
<p id = "variance"></p>
<p id = "sd"></p>

<script type = "text/javascript">

        var mean = 0;
        var median = 0;
        var count = length;
        var mode = 0;
        var variance = 0;
        var standard_deviation = 0;

        var meanOutput = document.getElementById('mean'); 
        var medianOutput = document.getElementById('median'); 
        var modeOutput = document.getElementById('mode'); 
        var countOutput = document.getElementById('count'); 
        var summationOutput = document.getElementById('summation'); 
        var varianceOutput = document.getElementById('variance'); 
        var sdOutput = document.getElementById('sd'); 

    function processData()
    {
        var arrayOfLines = document.getElementById('myTextArea').value.split('\n');

        var sum = findSum(arrayOfLines);
        findMean(arrayOfLines, sum);
        findMedian(arrayOfLines);
        findMode(arrayOfLines);
        findVariance(arrayOfLines);
        findStandardDeviation(arrayOfLines);
        findVariance(arrayOfLines);
    }

    function findSum (array)
    {
        var count = array.length;
        var sum = 0;

        for (var a = 0; a < array.length; a++)
        {
            sum += parseInt(array[a]);
        }

        countOutput.innerHTML = "Count: " + array.length; 
        summationOutput.innerHTML = "Sum: " + JSON.stringify(sum); 
        return sum;
    }

    function findMode (array)
    {
        var modeArr = [];
        var modeCounter = [];
        modeArr.length = array.length;
        modeCounter.length = array.length;

        for (var a = 0; a < array.length; a++)
        {   
            for (var b = 0; b < modeArr.length; b++)
            {
                if (modeArr[a] == modeArr[b])
                {
                    modeCounter[a]++;
                }

                if (a == 0)
                {
                    b--;
                }
            }

            modeArr[a] = array[a];
        }

        modeOutput.innerHTML = "Mode: "; 

    }


    function findMean (array, sum)
    {
        mean = sum/array.length;
        meanOutput.innerHTML = "Mean: " +  mean.toPrecision(2); 

    }




    function findMedian (array)
    {
        for(var i=0; i < array.length; i++) 
        {
            array[i] = +array[i];
        }

        var sortedArrayOfLines = array.sort(function(a, b){return a - b});

        if (array.length % 2 == 1)
        {
            median = sortedArrayOfLines[((array.length - 1)/2)]
        }

        else        
        {
            median = (sortedArrayOfLines[array.length/2] + sortedArrayOfLines[(array.length/2)+1])/2
        }

        medianOutput.innerHTML = "Median: " + median; 
    }

    function findVariance (array) 
    {
        var mean = mean(array);
        return mean(array.map(function(num) 
        {
            varianceOutput.innerHTML = Math.pow(num - mean, 2); 
        }));
    }

    function findStandardDeviation (array) 
    {
        medianOutput.innerHTML = Math.sqrt(variance(array)); 

    }

</script>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

所以问题不在于length它是一个无限循环。

问题在于这段代码

if (a == 0)
{
  b--;
}

这是在以下循环中,b作为迭代器。见下文。

for (var b = 0; b < modeArr.length; b++)
外部循环将

a设置为零。因此a==0在内循环中始终为真。 b永远不会只增加减少。因此,这是一个无限循环,因为b永远不会大于modeArr.length

所以我会考虑修改这个函数,下面是一个模式函数的可能候选者的例子:

Get the element with the highest occurrence in an array