Adding a comma at every third number character
<p>In my code I have a variable <code>myCash</code>, which is printed into an <code>h1</code> element using javaScript's <code>innerHTML</code>. I found a function online that puts a comma after every third character from the end of the number so that the number is easier to read. I've tried for a couple of hours now sending my variable <code>myCash</code> into the function and then print it on the screen. I CANNOT get it to work.</p>

<p>I've tried just alerting the new variable to the screen after page load or by pressing a button, but I get nothing and the alert doesn't even work. Here's the comma insert function:</p>

<pre><code>function commaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    amount = new String(amount);
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i &lt; 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length &gt; 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length &gt; 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length &lt; 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}
</code></pre>

<p>now when I want my variable to change I've tried it a few different ways including this:</p>

<p><code>var newMyCash = commaFormatted(myCash);
 alert(newMyCash);</code></p>

<p>and this:</p>

<p><code>alert(commaFormatted(myCash);</code></p>

<p>Where of course <code>myCash</code> equal some large number;</p>

<p>This does absolutely nothing! What am I doing wrong here??</p>
