LeftPad in Javascript
We'll cover the following...
Press + to interact
function leftPad (str, len, ch) {if (!ch) {ch = '.';}str = String(str);if (ch.length !== 1) {throw 'Invalid Input'}len = len - str.length;strCh = '';for (i = 0; i < len; ++i) {strCh += ch;}return (strCh + str);}console.log(leftPad('1', 1));console.log(leftPad('2', 2));console.log(leftPad('3', 3));console.log(leftPad('4', 4));console.log(leftPad('5', 5));console.log(leftPad('hello', 7));console.log(leftPad("foo", 6));console.log(leftPad("foo", 3));console.log(leftPad("foobar", 3));console.log(leftPad("foo", 6, "?"));
to save progress
Left-Pad could be the next FizzBuzz so we coded it up in 13 Languages
LeftPad in Python
to save progress