Created
March 30, 2016 01:13
-
-
Save jslnriot/38f4609e2a30d5ebec1a0d15fcd1b53e to your computer and use it in GitHub Desktop.
padLeft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.