-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request coffeescript-cookbook#6 from AaronW/master
Degrees and Radians
- Loading branch information
Showing
4 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
layout: recipe | ||
title: Converting Radians and Degrees | ||
chapter: Math | ||
--- | ||
|
||
h2. Problem | ||
|
||
You need to convert between radians and degrees. | ||
|
||
h2. Solution | ||
|
||
Use Javascript's Math.PI and a simple formula to convert between the two. | ||
|
||
{% highlight coffeescript %} | ||
# To convert from radians to degrees | ||
radiansToDegrees = (radians) -> | ||
degrees = radians * 180 / Math.PI | ||
|
||
radiansToDegrees(1) | ||
# => 57.29577951308232 | ||
|
||
# To convert from degrees to radians | ||
degreesToRadians = (degrees) -> | ||
radians = degrees * Math.PI / 180 | ||
|
||
degreesToRadians(1) | ||
# => 0.017453292519943295 | ||
{% endhighlight %} | ||
|
||
h2. Discussion | ||
|
||
Questions? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
layout: recipe | ||
title: Finding Substrings | ||
chapter: Strings | ||
--- | ||
|
||
h2. Problem | ||
|
||
You need to find the first or last occurrence of a search string within a message. | ||
|
||
h2. Solution | ||
|
||
Use Javascript's indexOf() and lastIndexOf() to find the first and last occurrences of a string, respectively. | ||
Syntax: string.indexOf(searchstring, start) | ||
|
||
{% highlight coffeescript %} | ||
message = "This is a test string. This has a repeat or two. This might even have a third." | ||
message.indexOf("This",0) | ||
# => 0 | ||
|
||
# Modifying the start parameter | ||
message.indexOf("This",5) | ||
# => 23 | ||
|
||
message.lastIndexOf("This") | ||
# => 49 | ||
|
||
{% endhighlight %} | ||
|
||
h2. Discussion | ||
|
||
Still need recipe to count occurrences of a given string within a message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
layout: recipe | ||
title: For Loops | ||
chapter: Syntax | ||
--- | ||
|
||
h2. Problem | ||
|
||
You need to iterate over an array, object or range with a for loop. | ||
|
||
h2. Solution | ||
|
||
{% highlight coffeescript %} | ||
# for(i = 1; i<= 10; i++) | ||
x for x in [1..10] | ||
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] | ||
|
||
# To count by 2 | ||
# for(i=1; i<= 10; i=i+2) | ||
x for x in [1..10] by 2 | ||
# => [ 1, 3, 5, 7, 9 ] | ||
|
||
# Perform a simple operation like squaring each item. | ||
x * x for x in [1..10] | ||
# = > [1,4,9,16,25,36,49,64,81,100] | ||
{% endhighlight %} | ||
|
||
h2. Discussion | ||
|
||
Comprehensions replace for loops in CoffeeScript, but they simply compile into the traditional javascript equivalent for-loop. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters