forked from shaonkabir8/JavaScript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-and-replace.js
More file actions
27 lines (18 loc) · 1.08 KB
/
search-and-replace.js
File metadata and controls
27 lines (18 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
🔥 Intermediate Algorithm Scripting: Search and Replace
⚡ Instruction: Perform a search and replace on the sentence using the arguments provided and return the new sentence.
✔ First argument is the sentence to perform the search and replace on.
✔ Second argument is the word that you will be replacing (before).
✔ Third argument is what you will be replacing the second argument with (after).
🌏 Refference: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace
*/
function searchAndReplace(string, before, after) {
// Check if the first letter of before (before[0]) is upperCase
// if before[0] is upperCase, then make after[0] upperCase
if (before[0] === before[0].toUpperCase()) {
after = after.replace(after[0], after[0].toUpperCase());
}
// 🔥 if both of them ( before[0] and after[0] ) are lowerCase,
return string.replace(before, after);
}
searchAndReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");