Skip to content

Commit 2eab366

Browse files
feat(curriculum): convert search and replace challenge to lab (freeCodeCamp#62129)
Co-authored-by: Ilenia <[email protected]>
1 parent ae6357f commit 2eab366

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

client/i18n/locales/english/intro.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,6 +3725,12 @@
37253725
"title": "Build a RegEx Sandbox",
37263726
"intro": ["In this lab you'll build a regex sandbox."]
37273727
},
3728+
"lab-smart-word-replacement": {
3729+
"title": "Build a Smart Word Replacement Function",
3730+
"intro": [
3731+
"In this lab, you will use regex to create a function that performs a search and replace operation on a given string."
3732+
]
3733+
},
37283734
"review-javascript-regular-expressions": {
37293735
"title": "JavaScript Regular Expressions Review",
37303736
"intro": [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Introduction to the Build a Smart Word Replacement Function
3+
block: lab-smart-word-replacement
4+
superBlock: full-stack-developer
5+
---
6+
7+
## Introduction to the Build a Smart Word Replacement Function
8+
9+
In this lab, you will use regex to create a function that performs a search and replace operation on a given string.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
id: a0b5010f579e69b815e7c5d6
3+
title: Build a Smart Word Replacement Function
4+
challengeType: 26
5+
dashedName: build-a-smart-word-replacement-function
6+
---
7+
8+
# --description--
9+
10+
In this lab, you will create a function that performs a search and replace on the sentence using the arguments provided and then returns the new sentence.
11+
12+
**Note:** 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`
13+
14+
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
15+
16+
**User Stories:**
17+
18+
1. You should create a function named `myReplace`.
19+
1. The `myReplace` function should take three arguments: a string, a word to be replaced, and the word to replace it with.
20+
1. The `myReplace` function should return the new string with the replacement.
21+
1. You should preserve the case of the first character in the original word when you are replacing it.
22+
23+
# --hints--
24+
25+
You should create a function named `myReplace`.
26+
27+
```js
28+
assert.isFunction(myReplace);
29+
```
30+
31+
`myReplace` should take three arguments.
32+
33+
```js
34+
assert.lengthOf(myReplace, 3);
35+
```
36+
37+
`myReplace("Let us go to the store", "store", "mall")` should return the string `Let us go to the mall`.
38+
39+
```js
40+
assert.deepEqual(
41+
myReplace('Let us go to the store', 'store', 'mall'),
42+
'Let us go to the mall'
43+
);
44+
```
45+
46+
`myReplace("He is Sleeping on the couch", "Sleeping", "sitting")` should return the string `He is Sitting on the couch`.
47+
48+
```js
49+
assert.deepEqual(
50+
myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting'),
51+
'He is Sitting on the couch'
52+
);
53+
```
54+
55+
`myReplace("I think we should look up there", "up", "Down")` should return the string `I think we should look down there`.
56+
57+
```js
58+
assert.deepEqual(
59+
myReplace('I think we should look up there', 'up', 'Down'),
60+
'I think we should look down there'
61+
);
62+
```
63+
64+
`myReplace("This has a spellngi error", "spellngi", "spelling")` should return the string `This has a spelling error`.
65+
66+
```js
67+
assert.deepEqual(
68+
myReplace('This has a spellngi error', 'spellngi', 'spelling'),
69+
'This has a spelling error'
70+
);
71+
```
72+
73+
`myReplace("His name is Tom", "Tom", "john")` should return the string `His name is John`.
74+
75+
```js
76+
assert.deepEqual(
77+
myReplace('His name is Tom', 'Tom', 'john'),
78+
'His name is John'
79+
);
80+
```
81+
82+
`myReplace("Let us get back to more Coding", "Coding", "algorithms")` should return the string `Let us get back to more Algorithms`.
83+
84+
```js
85+
assert.deepEqual(
86+
myReplace('Let us get back to more Coding', 'Coding', 'algorithms'),
87+
'Let us get back to more Algorithms'
88+
);
89+
```
90+
91+
# --seed--
92+
93+
## --seed-contents--
94+
95+
```js
96+
97+
```
98+
99+
# --solutions--
100+
101+
```js
102+
function myReplace(str, before, after) {
103+
if (before.charAt(0) === before.charAt(0).toUpperCase()) {
104+
after = after.charAt(0).toUpperCase() + after.substring(1);
105+
} else {
106+
after = after.charAt(0).toLowerCase() + after.substring(1);
107+
}
108+
return str.replace(before, after);
109+
}
110+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Build a Smart Word Replacement Function",
3+
"isUpcomingChange": false,
4+
"dashedName": "lab-smart-word-replacement",
5+
"helpCategory": "JavaScript",
6+
"blockLayout": "link",
7+
"blockType": "lab",
8+
"challengeOrder": [
9+
{
10+
"id": "a0b5010f579e69b815e7c5d6",
11+
"title": "Build a Smart Word Replacement Function"
12+
}
13+
],
14+
"usesMultifileEditor": true
15+
}

curriculum/structure/superblocks/full-stack-developer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@
454454
"lab-palindrome-checker",
455455
"lab-markdown-to-html-converter",
456456
"lab-regex-sandbox",
457+
"lab-smart-word-replacement",
457458
"review-javascript-regular-expressions",
458459
"quiz-javascript-regular-expressions"
459460
]

0 commit comments

Comments
 (0)