Javascript String padLeft(ch, n)
String.prototype.padLeft = function(ch, n) { return this.length >= n ? this.valueOf()/*from w w w. ja va2 s . c o m*/ : (Array(n + 1).join(ch) + this).slice(-n); };
/*//w ww . j a v a 2 s . c om You have to write two functions, padLeft and padRight that will fill the missing characters so the other system can import the data without any errors. Registry Number | Activity Code 00000000001337 | 1337000 60316817000448 | 6204000 */ 'use strict'; String.prototype.padLeft = function(ch, n) { let myString = ''; while(myString.length < n - this.length) { myString += ch; } myString += this; return myString; }; String.prototype.padRight = function(ch, n) { let myString = this; while(myString.length < n) { myString += ch; } return myString; }; console.log('5'.padLeft('0', 5)); console.log('2'.padRight('.', 2));
String.prototype.padLeft = function(ch, n) { if(this.length < n) { var diff = n - this.length; var tempArr = this.split(''); var tmp;//from w w w. j ava 2 s . co m for(var i = 0; i < diff; i++) { tempArr.unshift(ch); } tmp = tempArr.toString().replace(/,/g, ''); return tmp; } else if(parseInt(this).toString() !== 'NaN'){ return parseInt(this); } else { return this; } };