I am trying to get the standard deviation of a user input string. I have as follows, but it returns the wrong value for SD. The calculation should go as follows: Sum values/number values = mean Square (sum each value-mean) Sum squares/number values.
Assistance appreciated (and explanation if possible):
function sum() {
var val = document.getElementById('userInput').value;
var temp = val.split(" ");
var total = 0;
var v;
var mean = total / temp.length;
var total1 = 0;
var v1;
var temp23;
var square;
for (var i = 0; i < temp.length; i++) {
v = parseFloat(temp[i]);
total += v;
}
mean = total / temp.length;
for (var i = 0; i < temp.length; i++) {
v1 = parseFloat(Math.pow(temp[i] - mean), 2);
total1 += v1;
}
temp23 = total1 / temp.length;
square = Math.sqrt(temp23);
document.write(total + '<br />');
document.write(mean + '<br />');
document.write(square);
}
<html>
<head>
</head>
<body>
<form id="input">
<textarea id="userInput" rows=20 cols=20></textarea>
<input id="Run" type=Button value="run" onClick="sum()" />
</form>
</body>
</html>