Placing n Statistics above % Statistics

From Q
Jump to navigation Jump to search

Places the counts (e.g., n) above the percentage statistics.

// A helper function that allows moving an element within an array.
// Thanks to:
// http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};
// Wherever we find an 'n' (sample size) statistic, move it to the top of the list of statistics.
var stats = table.statistics;
// How many stats we've already moved so far.
var moved_stats = 0;
for (var i = 0; i < stats.length; i++) {
    if (stats[i] == 'n' || stats[i].substr(-2) == ' n') {
        stats.move(i, moved_stats);
        moved_stats++;
    }
}
table.statistics = stats;

See also