(重新发布,因为我以前的帖子由于某种原因无法被提升) 我目前有一个脚本,它从文本区域获取数字并使用它们进行各种计算。我的标准偏差函数有问题,因为它不会输出值,即使它的类型是数字,也是正数(.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);
}
以下是我简化的剧本:
<script type = "text/javascript">
var mean = 0;
var median = 0;
var count = length;
var mode = 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);
}
</script>
<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>
答案 0 :(得分:0)
variance
调用返回的 findVariance()
变量包含字符串"Variance: "
。您可以使用.match()
与RegExp
/[\d.]+/
匹配数字或数字后跟"."
后跟数字字符串和+
运算符以将字符串转换为数字
var mean = 0;
var median = 0;
var count = length;
var mode = 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.match(/[\d.]+/)[0]);
console.log(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>