Skip to content

Instantly share code, notes, and snippets.

@jslnriot
Created March 30, 2016 01:13
Show Gist options
  • Save jslnriot/38f4609e2a30d5ebec1a0d15fcd1b53e to your computer and use it in GitHub Desktop.
Save jslnriot/38f4609e2a30d5ebec1a0d15fcd1b53e to your computer and use it in GitHub Desktop.
padLeft
String.prototype.padLeft = function(padding, passedChar) {
if(passedChar === undefined) {
passedChar = " ";
}
var specialChar = passedChar;
var strLength = this.length;
var padLength = padding - strLength;
for(i = 1; i < padLength; i++){
specialChar += passedChar;
}
return specialChar+this;
};
var s = "a string";
var out = s.padLeft(40, '*');
console.log('Results' + '\n');
console.log(out);
console.log(out.length);
console.log(out === '********************************a string');
var out2 = s.padLeft(40);
console.log(out2.length);
console.log(out2);
console.log(out2 === ' a string');
// out === '********************************a string';
// out === ' a string';
@jslnriot
Copy link
Author

This takes a string and calls a function padLeft() which accepts 2 arguments. One for the length of the padding, and the second for the special character passed. It can take no argument as well, in that case, it will be set to blank spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment