-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_string_replace.js
More file actions
59 lines (47 loc) · 1.73 KB
/
03_string_replace.js
File metadata and controls
59 lines (47 loc) · 1.73 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
/*
Implement the below function
that replaces a character `match` with another character `replacement`
in a given text and returns a new string.
Examples:
replace('hello world', 'l', 'n') => 'henno world'
replace('no spaces in here', ' ', '_') => 'no_spaces_in_here'
replace('', 'd', 'e') => ''
**Your function must return a value**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function checkEqual(charA, charB) {
return charA === charB;
}
function replace(text, match, replacement) {
const string = text;
const charToReplace = match;
const replaceWith = replacement;
let newString = "";
for (let index = 0; index < string.length; index++) {
const isStringsEqual = checkEqual(string[index], charToReplace);
newString += isStringsEqual ? replaceWith : string[index];
}
return newString;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function testCharReplace(string, charToReplace, charToReplaceWith, expect) {
const actual = replace(string, charToReplace, charToReplaceWith);
const isPassed = actual === expect;
let message = "String Before Replacement: '" + string;
message += "',\nString After Replaced with '" + charToReplaceWith;
message += "' inplace of '" + charToReplace + "', gives '" + actual + "'\n";
console.log(getMark(isPassed), message);
}
function testAll() {
testCharReplace('a', 'a', 'o', 'o');
testCharReplace('a', 'a', '_', '_');
testCharReplace('a', 'a', '', '');
testCharReplace('a', '', 'b', 'a');
testCharReplace('hello world', 'l', 'n', 'henno wornd');
testCharReplace('no spaces in here', ' ', '_', 'no_spaces_in_here');
testCharReplace('', 'd', 'e', '');
}
testAll();