的JavaScript。 Math.sqrt给NaN一个正数?

时间:2017-09-30 16:02:32

标签: javascript

我目前有一个脚本,它从文本区域获取数字,并使用它们进行各种计算。我的标准偏差功能有问题,因为它不会输出值,即使它的类型是数字,也是正数(.56)。

当我运行此代码时

var variance = findVariance(array);
    variance = parseFloat(variance);
    alert(typeof variance);

结果是

  

值为.56(我知道,因为当我不使用parseFloat时,它返回.56作为字符串)。所以当我运行这段代码时,我感到很困惑

alert("variance " + variance);
alert("sd" + Math.sqrt(variance));

并且两个警报都返回

  

的NaN

这是创建textarea的代码,调用函数processData(它将myTextArea的输入收集到一个数组中,然后调用函数findStandardDeviation)如果你愿意,我也可以发布方差函数。

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

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

<p id = "sd"></p>

<script type = "text/javascript">

        var standard_deviation = 0;

        var sdOutput = document.getElementById('sd'); 

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


        findStandardDeviation(arrayOfLines);
    }

这是我可以管理的最简化的脚本形式:

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 countOutput = document.getElementById('count');
var sumOutput = document.getElementById('sum');
var varianceOutput = document.getElementById('variance');
var sdOutput = document.getElementById('sd');

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

  var sum = findSum(arrayOfLines);
  findMeanOutput(arrayOfLines, sum);
  findVariance(arrayOfLines);
  findStandardDeviation(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;
  sumOutput.innerHTML = "Sum: " + JSON.stringify(sum);
  sum = parseInt(sum);
  return sum;
}

function findMean(array) //Used for finding mean and then returning it
{
  var sum = 0;

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

  mean = sum / array.length;
  return mean;
}

function findMeanOutput(array, sum) //finds mean and then establishes it as the mean for users input
{
  mean = sum / array.length;
  meanOutput.innerHTML = "Mean: " + mean;
}

function findVariance(array) {
  var mean = findMean(array);

  var variance = "Variance: " + findMean(array.map(function(num) {
    return Math.pow(num - mean, 2);
  })).toFixed(2);

  varianceOutput.innerHTML = "Variance: " + variance;
  return variance;
}

function findStandardDeviation(array) {
  var variance = findVariance(array);
  variance = parseFloat(variance);
  alert(typeof variance);
  alert("variance " + variance);
  alert("sd" + Math.sqrt(variance));
  sdOutput.innerHTML = "Standard Deviation: " + Math.sqrt(variance).toFixed(2);
}
<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="count"></p>
      <p id="sum"></p>
      <p id="variance"></p>
      <p id="sd"></p>
</body>

</html>

3 个答案:

答案 0 :(得分:1)

.56不是整数,因此您不应使用parseIntparseFloat是您正在寻找的功能。或者更简单:Number

答案 1 :(得分:0)

试试这个:

在您的情况下.56是浮点数,因此请使用parseFloat

  1. parseInt - 将字符串转换为整数,如果你要浮动它,那么它会给0,
  2. parseFloat - 返回浮点数,

答案 2 :(得分:0)

我将字符串“Variance:”附加到方差变量以及实际方差,当转换为浮点数时仍会产生NaN。我通过交换它来解决它:

variance = parseFloat(variance);

用这个:

variance = parseFloat(+variance.match(/[\d.]+/)[0]);