Asad Mahmood Asad Mahmood - 8 months ago 20
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.


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