Skip to content

Commit 0e26c10

Browse files
committed
Complete search and replace algorithm
1 parent e48fe20 commit 0e26c10

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
1010
- [Wherefore Art Thou](#wherefore-art-thou)
1111
- [Spinal Tap Case](#spinal-tap-case)
1212
- [Pig Latin](#pig-latin)
13+
- [Search and Replace](#search-and-replace)
1314

1415
#### Sum All Numbers In a Range
1516

@@ -102,7 +103,35 @@ export function translatePigLatin(str) {
102103
return str + 'way';
103104
}
104105

105-
let splitStr = str.split(/([aeiou].*)/)
106+
const splitStr = str.split(/([aeiou].*)/);
106107
return [splitStr[1], splitStr[0], 'ay'].join('');
107108
}
108109
```
110+
111+
#### Search and Replace
112+
113+
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
114+
115+
First argument is the sentence to perform the search and replace on.
116+
117+
Second argument is the word that you will be replacing (before).
118+
119+
Third argument is what you will be replacing the second argument with (after).
120+
121+
Note
122+
Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog"
123+
124+
```javascript
125+
export function myReplace(str, before, after) {
126+
const indexOfInsert = str.indexOf(before);
127+
const preInsert = str.slice(0, indexOfInsert);
128+
const postInsert = str.slice(indexOfInsert + before.length);
129+
130+
if (/^[A-Z]/.test(before)) {
131+
const capitalizedAfter = after.charAt(0).toUpperCase() + after.slice(1);
132+
return [preInsert, capitalizedAfter, postInsert].join('');
133+
}
134+
135+
return [preInsert, after, postInsert].join('');
136+
}
137+
```

src/fcc-intermediate-algorithms/pig_latin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ export function translatePigLatin(str) {
22
if (str === "") {
33
return "";
44
}
5-
5+
66
if (/^[aeiou]/.test(str)) {
77
return str + 'way';
88
}
99

10-
let splitStr = str.split(/([aeiou].*)/)
10+
const splitStr = str.split(/([aeiou].*)/);
1111
return [splitStr[1], splitStr[0], 'ay'].join('');
1212
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function myReplace(str, before, after) {
2+
const indexOfInsert = str.indexOf(before);
3+
const preInsert = str.slice(0, indexOfInsert);
4+
const postInsert = str.slice(indexOfInsert + before.length);
5+
6+
if (/^[A-Z]/.test(before)) {
7+
const capitalizedAfter = after.charAt(0).toUpperCase() + after.slice(1);
8+
return [preInsert, capitalizedAfter, postInsert].join('');
9+
}
10+
11+
return [preInsert, after, postInsert].join('');
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { myReplace } from "../../src/fcc-intermediate-algorithms/search_and_replace";
2+
3+
test('should replace the specified word', () => {
4+
expect(myReplace("His name is Tom", "Tom", "john")).toBe("His name is John");
5+
expect(myReplace("The boat is red", "red", "blue")).toBe("The boat is blue");
6+
});

0 commit comments

Comments
 (0)