Formating characters – indentation&comma (stackoverflow problem)

Problem posted: 08/08/2015

url:  http://stackoverflow.com/questions/31890301/javascript-string-split-based-on-value/31890361#31890361
fiddle: https://jsfiddle.net/eugensunic/kphe5fbL/13/

Problem description:

Format the current string :
Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0″

to the following output:
Landing Hits: 0, Rewards Hits: 0, Facebook Posts: 0, Twitter Tweets: 0, Twitter Autofollows: 0, Instagram Photos: 0, Instagram Likes: 0, Instagram Votes: 0, Pinterest Pins: 0, Form Submissions: 0, Submissions: 0, Engagement: 0, Views: 0, Prints: 0″

The OP wanted to add a comma after the number (the number can be positive or negative greater than and lower than 0, not only 0)

Solution to the problem:

var c = "Landing Hits: -345Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 034534532Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";
var array = []
var counter = 0;
var num;
var dup = "";
for (var i = 0; i<c.length; i++) {
 dup += c[i];
 if (c[i]==":"){dup+=" ";}
 if (!isNaN(c[i]) && isNaN(c[i+1]) && c[i]!=" ") {
 dup += ", "
 }
}
 alert(dup);

After that I also decided to make a function which would get the length of each number after the colon ":" in the same sequence.

var c = "Landing Hits: 345Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 034534532Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";
var array = []
var counter = 0;
var num;
var dup = "";
var i=0;
for (var j=0; j<c.length; j++)
{
if (!isNaN(c[j]) && c[j]!=" ") // because js interprets " " as 0 if applied on isNan() evaluates to false so I hade to include the other part inside the if as well
{
counter++;
if (isNaN(c[j+1]))
{
array[i]=counter;
i++;
counter=0;
}
}
}
console.log(array[0]);