为什么我的html / js代码在我的函数上出错?

时间:2017-09-29 14:30:49

标签: javascript html

这是我得到的错误:

enter image description here

以下是代码:

<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">

    function processData()
    {
        var arrayOfLines = document.getElementById('myTextArea').value.split('\n'); 
        var length = arrayOfLines.length;
        var modeArr = {};
        var sum = 0;
        var mean = 0;
        var median = 0;
        var count = length;
        var mode = 0;
        var variance = 0;
        var standard deviation = 0;
        var modeCounter = {};

        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'); 

        alert("hi");
        alert(arrayOfLines[0]);
        sum(arrayOfLines);
        mean(arrayOfLines);
        median(arrayOfLines);
        mode(arrayOfLines);
        variance(arrayOfLines);
        standardDeviation(arrayOfLines);
        variance(arrayOfLines);
    }

    function sum(array)
    {
        for (var a = 0; a < length; a++)
        {
            sum += arrayOfLines[a];
        }
        alert(sum);
        summationOutput.innerHTML = sum; 

    }

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

            arr[a] = arrayOfLines[a];
        }

        moedOutput.innerHTML = mode; 

    }

    function mean (array)
    {
        mean = sum/length;
        meanOutput.innerHTML = mean; 

    }

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

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

        medianOutput.innerHTML = median; 
    }

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

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

    }

</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

问题在这里

var standard deviation = 0;
            ^^

替换为

var standard_deviation = 0;