Asad Mahmood Asad Mahmood - 1 year ago 58
HTML Question

How to make a single string into a multitude of strings?

I have a string called

which holds the string
. I want to add up all of those numbers up to make the number
I was considering doing a for loop for this however I do not know how to turn part of a string into its own value.
I anyone has any better idea of what to do please comment, or answer.

Answer Source

You could use String#split for the string and use Array#reduce for summing.

var e3 = '1,2,4,5,3,6',
    sum = e3.split(',').reduce(function (a, b) { 
      return a + +b; // +b forces b to number
    }, 0);


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