highly efficient solution #11
Changes from 3 commits
9c12fd7
24a33aa
81ca02c
5ff6fa8
2fca28d
1a90e0f
6ccfef5
34c1e5c
277ebb3
4bc455c
672d883
64f3b11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,18 @@ module.exports = leftpad; | |
function leftpad (str, len, ch) { | ||
str = String(str); | ||
|
||
var i = -1; | ||
|
||
if (!ch && ch !== 0) ch = ' '; | ||
|
||
len = len - str.length; | ||
if (len <=0) return str; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing whitespace before 0 |
||
|
||
while (++i < len) { | ||
str = ch + str; | ||
ch = ch + ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pad_str = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it's better to use camel-style, let's say |
||
while (true) { | ||
if (len & 1) pad_str += ch; | ||
len >>= 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing semicolon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for those comments. |
||
if (len) ch += ch; | ||
else break; | ||
} | ||
|
||
return str; | ||
return pad_str + str; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str = '' + str;
how about?
http://jsperf.com/number-to-string/2