Skip to content

Instantly share code, notes, and snippets.

@composite
Created January 13, 2014 07:58
Show Gist options
  • Select an option

  • Save composite/8396308 to your computer and use it in GitHub Desktop.

Select an option

Save composite/8396308 to your computer and use it in GitHub Desktop.
Which do you like more smallest script of 2 padLeft functions?
function pad1(str,char,loop){
var result = '';
for(var i=0;i<loop;i++) result+=char;
return (result+str).slice(-loop);
}
function pad2(str,char,loop){
for(var i=0,len=loop-str.length;i<len;i++)
str = char + str;
return str;
}
alert(pad1('12','0',4));
alert(pad2('12','0',4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment