Javascript范围,方差和标准偏差函数

时间:2016-06-07 00:04:41

标签: javascript html arrays

所以,我最近的任务是做一些计算并构建一个自定义的JS统计库。我剩下的唯一三件事就是创建范围,方差和标准差的函数。我在这里做的是将我的数组(x)传递给js函数,但它们一直是空白的。我做错了吗?



function findSum(x)
{
	var sum = 0;
	for(i = 0; i < x.length; i++)
	{
		sum = sum + x[i];
	}
	return sum;
};

function findMean(x)
{
return findSum(x) / x.length;
};

function findMedian(x) 
{
    x.sort( function(a,b) {return a - b;} );

    var half = Math.floor(x.length/2);

    if(x.length % 2)
        return x[half];
    else
        return (x[half-1] + x[half]) / 2.0;
}

// Ascending functions for sort
function ascNum(a, b) { return a - b; }

function clip(arg, min, max) {
  return Math.max(min, Math.min(arg, max));
};


function findMode(x) 
{
  var arrLen = x.length;
  var _arr = x.slice().sort(ascNum);
  var count = 1;
  var maxCount = 0;
  var numMaxCount = 0;
  var mode_arr = [];
  var i;

  for (i = 0; i < arrLen; i++) {
    if (_arr[i] === _arr[i + 1]) {
      count++;
    } else {
      if (count > maxCount) {
        mode_arr = [_arr[i]];
        maxCount = count;
        numMaxCount = 0;
      }
      // are there multiple max counts
      else if (count === maxCount) {
        mode_arr.push(_arr[i]);
        numMaxCount++;
      }
      // resetting count for new value in array
      count = 1;
    }
  }

  return numMaxCount === 0 ? mode_arr[0] : mode_arr;
};

function findRange(x)
{
	x.sort( function (a, b) {return a-b;} );
}

function findVariance(x) {
		var mean = findMean(x);
		return findMean(array.map(findSum(sum)) {
			return Math.pow(sum - mean, 2);
		}));
	},
	
function findStandardDeviation(x) 
{
		return Math.sqrt(findVariance(x));
};
&#13;
&#13;
&#13;

HTML代码:

    <html>
    <head>
    <h1>Statistical Calculations</h1>

    <title>Calculating Stats</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src='Stats.js'></script>
    <script language="JavaScript">
    function addNumber()
    {
        var input = document.getElementById('input').value;
        var list = document.getElementById('list');
        var option = document.createElement('OPTION');
        list.options.add(option);
        option.text = input;
    }

    function getStatistics()
    {
        var list = new Array();
        var select = document.getElementById('list');
        for(i = 0; i < select.options.length; i++)
        {
            list[i] = parseInt(select.options[i].text);
        }
        document.getElementById('summation').value =findSum(list);
        document.getElementById('mean').value = findMean(list);
        document.getElementById('median').value = findMedian(list);
        document.getElementById('mode').value = findMode(list);
        document.getElementById('variance').value = findVariance(list);
        document.getElementById('standardDev').value = findStandardDeviation(list);
        document.getElementById('range').value = findRange(list);
        document.getElementById('max').value = findMax(list);
        document.getElementById('min').value = findMin(list);
    }
    </script>
    </head>
    <body>
    <table>
    <tr>
        <td>Input Number:</td><td><input type='text' id='input'></td>
    </tr>
    <tr>
        <td colpsan='2'><input type='button' value='Add Number' onClick='addNumber()'></td>
    </tr>
    <tr>
        <td colspan='2'>
            <select id='list' size='5'>
            </select>
        </td>
    </tr>
        <tr>
                <td colpsan='2'><input type='button' value='Calculate!' onClick='getStatistics()'></td>
        </tr>
        <tr>
                <td>Summation:</td><td><input type='text' id='summation' readonly></td>
        </tr>
        <tr>
                <td>Mean:</td><td><input type='text' id='mean' readonly></td>
        </tr>
    <tr>
                <td>Median:</td><td><input type='text' id='median' readonly>    </td>
        </tr>
    <tr>
                <td>Mode:</td><td><input type='text' id='mode' readonly></td>
        </tr>
    <tr>               
         <td>Max:</td><td><input type='text' id='max' readonly></td>
        </tr>
    <tr>               
         <td>Min:</td><td><input type='text' id='min' readonly></td>
        </tr>


    <tr>
                <td>Range:</td><td><input type='text' id='range' readonly></td>
        </tr>
    <tr>
                <td>Variance:</td><td><input type='text' id='variance' readonly></td>
        </tr>
    <tr>
                <td>Standard Deviation:</td><td><input type='text' id='standardDev' readonly></td>
        </tr>


</table>
</body>
</html>

最后3个似乎完全没有做任何事情,过去几天我一直在抨击我的想法。如果有人能帮助我将功能分类到正常工作状态,那么我们将不胜感激!我确定数组已正确传递给函数,因为前4个函数显然有效。

0 个答案:

没有答案