-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg07-searchAndReplace.js
More file actions
172 lines (152 loc) · 6.09 KB
/
alg07-searchAndReplace.js
File metadata and controls
172 lines (152 loc) · 6.09 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const assert = require('assert');
/*
Source: <https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace>
Search and Replace
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).
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
*/
function isMyReplace(str, before, after) {
let newAfterStr;
if(/^[A-Z]/.test(before)) {
newAfterStr = after.split('').map((letter, index) => (index === 0) ? letter.toUpperCase() : letter).join('');
} else {
newAfterStr = after.toLowerCase();
}
const regex = RegExp(before, 'g');
return str.replace(regex, newAfterStr);
}
const fox = isMyReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
const letUs = isMyReplace('Let us go to the store', 'store', 'mall');
const heIsSleeping = isMyReplace("He is Sleeping on the couch", "Sleeping", "sitting");
const iThink = isMyReplace("I think we should look up there", "up", "Down");
const spellngi = isMyReplace("This has a spellngi error", "spellngi", "spelling");
const nameTom = isMyReplace("His name is Tom", "Tom", "john");
const moreCoding = isMyReplace("Let us get back to more Coding", "Coding", "algorithms");
assert.strictEqual(fox, 'A quick brown fox leaped over the lazy dog');
assert.strictEqual(letUs, 'Let us go to the mall');
assert.strictEqual(heIsSleeping, 'He is Sitting on the couch');
assert.strictEqual(iThink, 'I think we should look down there');
assert.strictEqual(spellngi, 'This has a spelling error');
assert.strictEqual(nameTom, 'His name is John');
assert.strictEqual(moreCoding, 'Let us get back to more Algorithms');
/*
Get a help > Get a hint <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-search-and-replace/16045>
*/
//Solution 1
function myReplace1(str, before, after) {
// Find index where before is on string
var index = str.indexOf(before);
// Check to see if the first letter is uppercase or not
if (str[index] === str[index].toUpperCase()) {
// Change the after word to be capitalized before we use it.
after = after.charAt(0).toUpperCase() + after.slice(1);
} else {
// Change the after word to be uncapitalized before we use it.
after = after.charAt(0).toLowerCase() + after.slice(1);
}
// Now replace the original str with the edited one.
str = str.replace(before, after);
return str;
}
//Solution 2
function myReplace2(str, before, after) {
// Check if first character of argument "before" is a capital or lowercase letter and change the first character of argument "after" to match the case
if (/^[A-Z]/.test(before)) {
after = after[0].toUpperCase() + after.substring(1)
} else {
after = after[0].toLowerCase() + after.substring(1)
}
// return string with argument "before" replaced by argument "after" (with correct case)
return str.replace(before, after);
}
//Solution 3
function myReplace3(str, before, after) {
// create a function that will change the casing of any number of letter in parameter "target"
// matching parameter "source"
function applyCasing(source, target) {
// split the source and target strings to array of letters
var targetArr = target.split("");
var sourceArr = source.split("");
// iterate through all the items of sourceArr and targetArr arrays till loop hits the end of shortest array
for (var i = 0; i < Math.min(targetArr.length, sourceArr.length); i++) {
// find out the casing of every letter from sourceArr using regular expression
// if sourceArr[i] is upper case then convert targetArr[i] to upper case
if (/[A-Z]/.test(sourceArr[i])) {
targetArr[i] = targetArr[i].toUpperCase();
}
// if sourceArr[i] is not upper case then convert targetArr[i] to lower case
else targetArr[i] = targetArr[i].toLowerCase();
}
// join modified targetArr to string and return
return targetArr.join("");
}
// replace "before" with "after" with "before"-casing
return str.replace(before, applyCasing(before, after));
}
//Solution 4
String.prototype.capitalize =
String.prototype.capitalize ||
function() {
return this[0].toUpperCase() + this.slice(1);
};
const Util = (function() {
// Create utility module to hold helper functions
function textCase(str, tCase) {
// Depending if the tCase argument is passed we either set the case of the
// given string or we get it.
// Those functions can be expanded for other text cases.
if (tCase) {
return setCase(str, tCase);
} else {
return getCase(str);
}
function setCase(str, tCase) {
switch (tCase) {
case "uppercase":
return str.toUpperCase();
case "lowercase":
return str.toLowerCase();
case "capitalized":
return str.capitalize();
default:
return str;
}
}
function getCase(str) {
if (str === str.toUpperCase()) {
return "uppercase";
}
if (str === str.toLowerCase()) {
return "lowercase";
}
if (str === str.capitalize()) {
return "capitalized";
}
return "normal";
}
}
return {
textCase
};
})();
function myReplace4(str, before, after) {
const { textCase } = Util;
const regex = new RegExp(before, "gi");
const replacingStr = textCase(after, textCase(before));
return str.replace(regex, replacingStr);
}
//Solution 5
function myReplace5(str, before, after) {
const myArr = str.split(" ");
const [wordToReplace] = myArr.filter(item => item === before);
return wordToReplace[0].toUpperCase() !== wordToReplace[0]
? myArr.map(item => (item === before ? after : item)).join(" ")
: myArr
.map(item =>
item === before ? after[0].toUpperCase() + after.slice(1) : item
)
.join(" ");
}