-1

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:

enter image description here

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?

Bruno Toffolo
  • 1,424
  • 17
  • 24
Mike
  • 69
  • 1
  • 7
  • 3
    [You need to add your code (not an image) to the question.](https://stackoverflow.com/help/mcve) – Andy Apr 04 '16 at 00:41
  • You also want to add the expected result because it's not clear what you need to display. – Andy Apr 04 '16 at 00:56
  • Note: [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Jonathan Lonowski Apr 04 '16 at 00:57
  • sorry i will do that next time, this is my first time using this website – Mike Apr 04 '16 at 01:12

2 Answers2

2

You can use the arr.split("") function to achieve this. First by grabbing what is inside the element, and then splitting it into an array. Here is a JSFiddle for an example:

https://jsfiddle.net/mvvr8jja/

I did not do this is my example, but you would want to convert the items in the array to integers using parseInt() before you could perform arithmetic because they are currently string values.

theblindprophet
  • 7,260
  • 5
  • 29
  • 49
0

It looks like you want to 1) find the sum of the numbers, 2) find the squares of the individual numbers, and 3) find the average of the sum of numbers. You are not going to be able to do that with that loop, certainly with respect to the numbers.

Here's an alternative method that you might find useful:

Helper function to return an element given an id.

var Q = function (id) { return document.getElementById(id); };

Takes the textContent of divout, split it into an array, then for each array coerce the string to an integer.

var arr = Q('divout').textContent.split(',').map(Number);

Two functions for use with map and reduce to find the sum and square of the numbers in the array.

var sumNumbers = function (p, c) { return p + c; };
var squareNumbers = function (x) { return x * x; };

Find the sum, square, and avg of the numbers. map returns a new array, reduce returns an integer.

var sum = arr.reduce(sumNumbers);
var square = arr.map(squareNumbers);
var avg = sum / arr.length;

Add the result of the operations to the DOM.

Q('sum').textContent = sum; // 32
Q('square').textContent = square; // 0,1,9,49,441
Q('avg').textContent = avg; // 6.4

DEMO

Andy
  • 39,764
  • 8
  • 53
  • 80