Skip to content

Commit 6f60ee2

Browse files
committed
Finding substrings from wanted-recipes started, other fixes
1 parent 140f748 commit 6f60ee2

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

chapters/math/radians-degrees.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ radiansToDegrees = (radians) ->
2020
radiansToDegrees(1)
2121
# => 57.29577951308232
2222

23-
# To convert from degrees to radians (Answer is for 1)
23+
# To convert from degrees to radians
2424
degreesToRadians = (degrees) ->
2525
radians = degrees * Math.PI / 180
2626

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.

wanted-recipes.textile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ h2. Objects
1919

2020
h2. Strings
2121

22-
* Find a substring in a string # not by regex! JS indexOf(), lastIndexOf()
2322
* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl!
2423
* Splitting a string # JS "foo bar baz".split ' ' # => [ 'foo', 'bar', 'baz' ]
2524
* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y]

0 commit comments

Comments
 (0)