-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.problemSecretChat.js
More file actions
58 lines (49 loc) · 1.43 KB
/
09.problemSecretChat.js
File metadata and controls
58 lines (49 loc) · 1.43 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
function secretChat(input) {
let text = input.shift();
let commands = { Reverse, ChangeAll, InsertSpace };
while (input[0] !== "Reveal") {
let tokens = input.shift().split(":|:");
let name = tokens[0];
let p1 = tokens[1];
let p2 = tokens[2];
let command = commands[name];
text = command(text, p1, p2);
}
console.log(`You have a new text message: ${text}`)
function ChangeAll(text, substr, replacement) {
let result = text.split(substr).join(replacement);
console.log(result);
return result;
}
function Reverse(text, substr) {
let index = text.indexOf(substr);
if (index !== -1) {
let leftText = text.substring(0, index);
let rightText = text.substring(index + substr.length);
let result = leftText + rightText + substr.split("").reverse().join("");
console.log(result);
return result;
} else {
console.log("error");
return text;
}
}
function InsertSpace(text, index) {
let leftText = text.substring(0, index);
let rightText = text.substring(index);
let result = leftText + " " + rightText;
console.log(result);
return result;
}
}
secretChat([
"heVVodar!gniV",
"ChangeAll:|:V:|:l",
"Reverse:|:!gnil",
"InsertSpace:|:5",
"Reveal",
]);
//ChangeAl('heVVodar!gniV', 'V', 'l');
//Reverse("!abcOLLEHdef", "OLLEH");
//InsertSpace("hellodarling!", 5);
/**/