-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapparently.js
37 lines (27 loc) · 1.25 KB
/
apparently.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
// For every string, after every occurrence of 'and' and/or 'but', insert the substring 'apparently' directly after the occurrence(s).
// If input does not contain 'and' or 'but', return the same string. If a blank string, return ''.
// If substring 'apparently' is already directly after an 'and' and/or 'but', do not add another. (Do not add duplicates).
// Examples:
// Input 1
// 'It was great and I've never been on live television before but sometimes I don't watch this.'
// Output 1
// 'It was great and apparently I've never been on live television before but apparently sometimes I don't watch this.'
// Input 2
// 'but apparently'
// Output 2
// 'but apparently'
// (no changes because 'apparently' is already directly after 'but' and there should not be a duplicate.)
// An occurrence of 'and' and/or 'but' only counts when it is at least one space separated. For example 'andd' and 'bbut' do not count as occurrences, whereas 'b but' and 'and d' does count.
function apparently(string) {
if (!string) return "";
return string
.split(" ")
.map((val, i, arr) => {
if ((val === "and" || val === "but") && arr[i + 1] !== "apparently") {
return val + " apparently";
} else {
return val;
}
})
.join(" ");
}