Skip to content

Commit 03a46f3

Browse files
committed
add recipe for uppercasing a string
1 parent 7bd773d commit 03a46f3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: recipe
3+
title: Uppercasing a String
4+
chapter: Strings
5+
---
6+
## Problem
7+
8+
You want to uppercase a string.
9+
10+
## Solution
11+
12+
Use JavaScript's String toUpperCase() method:
13+
14+
{% highlight coffeescript %}
15+
"one two three".toUpperCase()
16+
# => 'ONE TWO THREE'
17+
{% endhighlight %}
18+
19+
## Discussion
20+
21+
`toUpperCase()` is a standard JavaScript method. Don't forget the parentheses.
22+
23+
### Syntax Sugar
24+
25+
You can add some Ruby-like syntax sugar with the following shortcut:
26+
27+
{% highlight coffeescript %}
28+
String::upcase = -> @toUpperCase()
29+
"one two three".upcase()
30+
# => 'ONE TWO THREE'
31+
{% endhighlight %}
32+
33+
The snippet above demonstrates a few features of CoffeeScript:
34+
35+
* The double-colon `::` is shorthand for saying `.prototype.`
36+
* The "at" sign `@` is shorthand for saying `this.`
37+
38+
The code above compiles in to the following JavaScript:
39+
40+
{% highlight javascript %}
41+
String.prototype.upcase = function() {
42+
return this.toUpperCase();
43+
};
44+
"one two three".upcase();
45+
{% endhighlight %}

0 commit comments

Comments
 (0)