2018-10-04 14:37:37 +01:00
---
id: a0b5010f579e69b815e7c5d6
title: Search and Replace
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16045
2021-01-13 03:31:00 +01:00
dashedName: search-and-replace
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-10-04 14:37:37 +01:00
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
2020-11-27 19:02:05 +01:00
2018-10-04 14:37:37 +01:00
First argument is the sentence to perform the search and replace on.
2020-11-27 19:02:05 +01:00
2018-10-04 14:37:37 +01:00
Second argument is the word that you will be replacing (before).
2020-11-27 19:02:05 +01:00
2018-10-04 14:37:37 +01:00
Third argument is what you will be replacing the second argument with (after).
2021-03-02 16:12:12 -08:00
**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`
2020-11-27 19:02:05 +01:00
# --hints--
2021-03-02 16:12:12 -08:00
`myReplace("Let us go to the store", "store", "mall")` should return the string `Let us go to the mall` .
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
myReplace('Let us go to the store', 'store', 'mall'),
'Let us go to the mall'
);
2018-10-04 14:37:37 +01:00
```
2021-03-02 16:12:12 -08:00
`myReplace("He is Sleeping on the couch", "Sleeping", "sitting")` should return the string `He is Sitting on the couch` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting'),
'He is Sitting on the couch'
);
```
2018-10-04 14:37:37 +01:00
2021-03-02 16:12:12 -08:00
`myReplace("I think we should look up there", "up", "Down")` should return the string `I think we should look down there` .
2018-10-04 14:37:37 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(
myReplace('I think we should look up there', 'up', 'Down'),
'I think we should look down there'
);
```
2018-10-04 14:37:37 +01:00
2021-03-02 16:12:12 -08:00
`myReplace("This has a spellngi error", "spellngi", "spelling")` should return the string `This has a spelling error` .
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
myReplace('This has a spellngi error', 'spellngi', 'spelling'),
'This has a spelling error'
);
2018-10-04 14:37:37 +01:00
```
2021-03-02 16:12:12 -08:00
`myReplace("His name is Tom", "Tom", "john")` should return the string `His name is John` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
myReplace('His name is Tom', 'Tom', 'john'),
'His name is John'
);
```
2018-10-04 14:37:37 +01:00
2021-03-02 16:12:12 -08:00
`myReplace("Let us get back to more Coding", "Coding", "algorithms")` should return the string `Let us get back to more Algorithms` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
myReplace('Let us get back to more Coding', 'Coding', 'algorithms'),
'Let us get back to more Algorithms'
);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
function myReplace(str, before, after) {
return str;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
```
# --solutions--
2018-10-04 14:37:37 +01:00
```js
function myReplace(str, before, after) {
if (before.charAt(0) === before.charAt(0).toUpperCase()) {
after = after.charAt(0).toUpperCase() + after.substring(1);
} else {
after = after.charAt(0).toLowerCase() + after.substring(1);
}
return str.replace(before, after);
}
```