I have this code that requests the graph API from Facebook to respond with a shared count for a particular URL (could be multiple on the page). Graph returns a full number (135934) and I take that number and abbreviate it into more friendly numbers (135K). Basically I'm looking for any help on improving the code to be more concise or if there's a better way to do something then to learn how.   <div>      <section data-url=url-to-this-article>          <h3> Some Article </h3>         <div class=box-counter></div>       </section>      <section data-url=url-to-this-article2>          <h3> Some Article 2</h3>         <div class=box-counter></div>       </section>      <section data-url=url-to-this-article3>          <h3> Some Article 3</h3>         <div class=box-counter></div>       </section>  </div><script>    jQuery(function(){            var theShares=[],            theNewShares=[];            var numb;            if(typeof jQuery==='undefined'){                console.log('need jQuery');            }else{                var c = document.getElementsByClassName('box-counter');                numb = c.length;                for (var i = 0; i < numb; i++) {                    var tempURL = c[i].parentNode.attributes['data-url'].nodeValue;                    jQuery.getJSON('http://graph.facebook.com/'+tempURL,function(data) {                        theShares.push(data.shares);                        insertDuring();                    });                }            }            function insertDuring(){                if(numb == theShares.length){                    var c = document.getElementsByClassName('box-counter');                    for (var i = 0; i < numb; i++) {                        var myString = theShares[i].toString();                        if(theShares[i] >= 1000){                            if(theShares[i] >= 10000){                                if(theShares[i] >= 100000){                                    if(theShares[i] >= 1000000){                                        var diff = myString.length - 6;                                        abridgeNumbers(myString,diff,m,i,false);                                    }else{                                        abridgeNumbers(myString,3,k,i,false);                                                                     }                                }else{                                    abridgeNumbers(myString,2,k,i,false);                                }                            }                            else{                                abridgeNumbers(myString,2,k,i,true);                                                      }                        }else{                            theNewShares[i] = theShares[i];                        }                        c[i].innerHTML = theNewShares[i];                    }                }            }            function abridgeNumbers(theString,theLimit,theAbbrev,i,k){                for (var j = 0; j < theLimit; j++) {                    if(typeof theNewShares[i] === 'undefined'){                        theNewShares[i] = theString.charAt(j);                    }else{                        if(k == true){                            theNewShares[i] = theNewShares[i] +.+ theString.charAt(j);                        }else{                            theNewShares[i] = theNewShares[i] + theString.charAt(j);                        }                    }                };                theNewShares[i] = theNewShares[i] + theAbbrev;            }        });</script>
Grab Facebook Graph share count and abbreviate it
javascript
Here is another solution, built in pure JS as you asked.(I didn't reproduce the (unchanged) HTML part, so it's easier to focus on it)<script>var counters = document.getElementsByClassName('box-counter');for (var i = 0, n = counters.length; i < n; i++) {    var element = counters[i],        XHR = new XMLHttpRequest();    element.innerHTML = 'unknown';    XHR.onreadystatechange = function() {        if (XHR.readyState == 4 && XHR.status == 200) {            element.innerHTML = friendlyExpress(                JSON.parse(responseText).shares            );        }    }    XHR.open(        'GET',         'http://graph.facebook.com/'        + counters[i].parentNode.attributes['data-url'].value,        /*async*/true);    XHR.send();}function friendlyExpress(x, legend, power) {    legend |= 'okmgpt';    power |= 1024;    var powerLimit = legend.length - 1,        powerIndex = 0;    while (x > (power - 1) && powerIndex < powerLimit) {      x /= power;      powerIndex++;    }    return Math.round(size) + legend[powerIndex];}</script>There are some points I think I must explain to be clear:regarding the whole code: since we no longer use jQuery, there is nothing to isolate this widget nor to ensure it waits loading complete to work: it is up to you to decide how to organize that depending on the other HTML and JS parts in your real application.regarding the process strategy: I realized you didn't finally get anything else than displaying the shares numbers in each .box-counter elements.So I dramatically simplified it, since we don't need intermediate variables like theShares, theNewShares and so onregarding Facebook Graph API: I'm not familiar with it at all, but I wanted to know a little, and I was surprised I couldn't find any reference to a member like shares in any returned object!Are you sure of what you're doing with it? May be you'll find this post of interest.Anyway I didn't changed anything to the way you used about that (and obviously couldn't test with true URLs).  regarding the share numbers conversion: BTW I seised the opportunity to enhance it a little (it's a kind of fantasy :-), so it may be reused in many other contexts. It 's why I keeped it as a separate function.For the rest, I hope it is self-explanatory, but feel free to ask for precisions.