Left/Right justified String - Node.js String

Node.js examples for String:Padding

Description

Left/Right justified String

Demo Code


String.prototype.leftJustified = function(n, fill) {

    fill = fill || " ";
    var padding = "";
    for (var i = n - this.length; i > 0; i--) {
        padding += fill;//from   ww w .jav a 2 s  . c  om
    }
    return this + padding;
};

String.prototype.rightJustified = function(n, fill) {

    fill = fill || " ";
    var padding = "";
    for (var i = n - this.length; i > 0; i--) {
        padding += fill;
    }
    return padding + this;
};

Related Tutorials