gpt4 book ai didi

JavaScript。 Math.sqrt 将 NaN 赋予正数?

转载 作者:行者123 更新时间:2023-12-03 03:22:34 25 4
gpt4 key购买 nike

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

当我运行这段代码时

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

结果是

number

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

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

两个警报均返回

NaN

这是创建文本区域的代码,调用函数 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>

最佳答案

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

关于JavaScript。 Math.sqrt 将 NaN 赋予正数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com