-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwednesday-professional.js
59 lines (47 loc) · 1.98 KB
/
wednesday-professional.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Search and Replace
//Perform a search and replace the sentence using the arguments provided and return the new sentence.
//The first argument is the sentence to perform the search and replace on.
//The second argument is the word that you will be replacing (before).
//The third argument is what you will be replacing the second argument with (after).
//define a function that takes 3 strings as parameters
//transform string into an array using string methods
//for loop to iterate
//condition to check the word match
//condition true check the word with capital letter
//condition false replace word with new word
//t-diagram
//index:i=0, i=1
//condition 1: arr[0]= 'quick', arr[1]= 'Brown'
// condition 2: false 'quick' (not a capital letter), 'brown' true (its capital letter)
//result: 'quick Yellow fox ...
let st = "quick Brown Fox jumped over the lazy dog";
function SearchAndReplace(string, before, after) {
let arr = string.trim().split(" "); //['quick', 'Brown', 'Fox', 'jumped', 'over', 'the', 'lazy', 'dog']
for (let i = 0; i < arr.length; i++) {
// arr[0]= 'quick', arr[1]='brown', arr[2]=''fox'
if (arr[i].toLowerCase() == before.toLowerCase()) {
// arr[i]= 'Brown' == before.toLowercase() 'brown' if its true it will be replaced to 'Brown'
if (arr[i][0] == arr[i][0].toUpperCase()) {
arr[i] = after[0].toUpperCase() + after.slice(1);
} else {
arr[i] = after;
}
}
}
return arr.join(" ");
}
console.log(SearchAndReplace(st, "Brown", "Yellow"));
// let str= "quick Brown Fox jumped over the lazy dog"
// function SearchAndReplace(string, before, after){
// let arr= str.trim().split(' ')
// for(let i=0; i< str.length; i++){
// if(arr[i].toLowerCase() == before.toLowerCase()){
// if(arr[i][0] == arr[1][0].toUpperCase)
// arr[i] = arr[0].toUpperCase + after.slice(1)
// }else {
// arr[1]=after;
// }
// }
// return arr.join("")
// }
// console.log(SearchAndReplace(str, "Brown", "Yellow"));