Parker Parker - 3 years ago 154
HTML Question

JS. Unable to output float with alert()

(repost because my former post was unable to get bumped up for some reason)
I currently have a script that takes numbers from a text area and does all sorts of calculations with them. I am having trouble with my standard deviation function because it will not output the value even though it's type is number, and it is positive (.56).

When I run this code

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


the result is


number


And the value is .56 (which I know because when I don't use parseFloat it returns .56 as a string). So I am baffled when I run this code

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


and both alerts return

NaN


This is the code that creates the textarea, calls the function processData( which gathers the input from myTextArea into an array and then calls the function findStandardDeviation) I can post the variance function as well if you'd like.

<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);
}


And here is the my reduced script:



<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>




Answer Source

variance variable returned from findVariance() call includes the string "Variance: ". You can use .match() with RegExp /[\d.]+/ to match digits or digits followed by "." followed by digit within string and + operator to convert string to number

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download