I have a bunch of numbers separated by commas in HTML as a string. I want to convert the numbers into an array in JavaScript and then use the numbers in the array to do functions such as adding and finding the average.
My page is like the following:
And my current code is:
<script>
var number = document.getElementById("divout")
//up here i want to create an array and use the array in the function below.
var sum = 0;
var average = 0;
var squared = 0;
function math(x)
{
document.write("amount of numbers=" + x.length + "<br>")
for (var i = 0; i <= x.length - 1; i++)
{
sum = sum+a[i];
squared = squared+(x[i]*x[i]);
average = sum/x.length;
}
}
math(number)
document.write("sum="+ sum + "<br>")
document.write("average-" + average + "<br>")
document.write("squared=" + squared + "<br>")
</script>
All Divisors:
<br>
<textarea rows="10" cols="50" placeholder="Divisors" id="divout">0,1,3,7,21</textarea>
<br>
(Source)
How can I do it?