Skip to content

Commit 42e3340

Browse files
committed
Merge branch 'neatcoding-master'
2 parents cb456b2 + 09e6644 commit 42e3340

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: chapters/strings/repeating.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Array(11).join 'foo'
1818
# => "foofoofoofoofoofoofoofoofoofoo"
1919
{% endhighlight %}
2020

21+
## Repeat method for Strings
22+
23+
You could also create a method for this in String prototype. It is as simple as that:
24+
25+
{% highlight coffeescript %}
26+
# add repeat method for all strings, that returns string repeated n times
27+
String::repeat = (n) -> Array(n+1).join(this)
28+
{% endhighlight %}
29+
2130
## Discussion
2231

2332
JavaScript lacks a string repeat function, as does CoffeeScript. List comprehensions and maps can be pressed into service here, but in the case of a simple string repeat it's easier to simply build an array of n+1 nulls and then glue them together.

0 commit comments

Comments
 (0)