Skip to content

Commit 8a89b59

Browse files
committed
Merge pull request coffeescript-cookbook#6 from AaronW/master
Degrees and Radians
2 parents b58b6b8 + 6f60ee2 commit 8a89b59

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: recipe
3+
title: Converting Radians and Degrees
4+
chapter: Math
5+
---
6+
7+
h2. Problem
8+
9+
You need to convert between radians and degrees.
10+
11+
h2. Solution
12+
13+
Use Javascript's Math.PI and a simple formula to convert between the two.
14+
15+
{% highlight coffeescript %}
16+
# To convert from radians to degrees
17+
radiansToDegrees = (radians) ->
18+
degrees = radians * 180 / Math.PI
19+
20+
radiansToDegrees(1)
21+
# => 57.29577951308232
22+
23+
# To convert from degrees to radians
24+
degreesToRadians = (degrees) ->
25+
radians = degrees * Math.PI / 180
26+
27+
degreesToRadians(1)
28+
# => 0.017453292519943295
29+
{% endhighlight %}
30+
31+
h2. Discussion
32+
33+
Questions?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: recipe
3+
title: Finding Substrings
4+
chapter: Strings
5+
---
6+
7+
h2. Problem
8+
9+
You need to find the first or last occurrence of a search string within a message.
10+
11+
h2. Solution
12+
13+
Use Javascript's indexOf() and lastIndexOf() to find the first and last occurrences of a string, respectively.
14+
Syntax: string.indexOf(searchstring, start)
15+
16+
{% highlight coffeescript %}
17+
message = "This is a test string. This has a repeat or two. This might even have a third."
18+
message.indexOf("This",0)
19+
# => 0
20+
21+
# Modifying the start parameter
22+
message.indexOf("This",5)
23+
# => 23
24+
25+
message.lastIndexOf("This")
26+
# => 49
27+
28+
{% endhighlight %}
29+
30+
h2. Discussion
31+
32+
Still need recipe to count occurrences of a given string within a message.

chapters/syntax/for_loops.textile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: recipe
3+
title: For Loops
4+
chapter: Syntax
5+
---
6+
7+
h2. Problem
8+
9+
You need to iterate over an array, object or range with a for loop.
10+
11+
h2. Solution
12+
13+
{% highlight coffeescript %}
14+
# for(i = 1; i<= 10; i++)
15+
x for x in [1..10]
16+
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
17+
18+
# To count by 2
19+
# for(i=1; i<= 10; i=i+2)
20+
x for x in [1..10] by 2
21+
# => [ 1, 3, 5, 7, 9 ]
22+
23+
# Perform a simple operation like squaring each item.
24+
x * x for x in [1..10]
25+
# = > [1,4,9,16,25,36,49,64,81,100]
26+
{% endhighlight %}
27+
28+
h2. Discussion
29+
30+
Comprehensions replace for loops in CoffeeScript, but they simply compile into the traditional javascript equivalent for-loop.

wanted-recipes.textile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ In the notes below, "JS" means the recipe is just a simple passthrough to an exi
1010

1111
h2. Syntax
1212

13-
* loops: for x in [1..10]
14-
* loops: for x in [1..10] then do_one_thing
1513
* list comp gotcha: y = x for x in [1..3]; y # => 3; but y = (x for x in [1..3]); y # => [ 1, 2, 3 ]
1614
* Ensuring variables are closed over # with "do"
1715

@@ -21,7 +19,6 @@ h2. Objects
2119

2220
h2. Strings
2321

24-
* Find a substring in a string # not by regex! JS indexOf(), lastIndexOf()
2522
* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl!
2623
* Splitting a string # JS "foo bar baz".split ' ' # => [ 'foo', 'bar', 'baz' ]
2724
* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y]
@@ -59,7 +56,6 @@ h2. Dates and Times
5956

6057
h2. Math
6158

62-
* Converting between degrees and radians
6359
* square root # JS Math.sqrt
6460
* Constants # JS Math.PI, Math.E
6561
* floor, ceil, round # JS Math.floor, Math.ceil, Math.round

0 commit comments

Comments
 (0)