-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip-comments.js
More file actions
53 lines (41 loc) · 1.21 KB
/
Copy pathstrip-comments.js
File metadata and controls
53 lines (41 loc) · 1.21 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
/* 4 kyu
Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.
Example:
Given an input string of:
apples, pears # and bananas
grapes
bananas !apples
The output expected would be:
apples, pears
grapes
bananas
The code would be called like so:
var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
// result should == "apples, pears\ngrapes\nbananas"
*/
function solution(input, markers) {
const arr = input.split('');
let startCommentIdx = [];
arr.forEach((item, idx) => {
markers.forEach((marker) => {
if (item === marker) {
return startCommentIdx.push(idx);
}
});
return;
});
const comments = startCommentIdx.map((item) => {
const escapes = '\n';
const endOfCommentIdx =
input.indexOf(escapes, item) !== -1
? input.indexOf(escapes, item)
: input.length;
return input.substring(item - 1, endOfCommentIdx);
});
let solution = input;
comments.reverse().forEach((comm) => {
const stringWithoutComment = solution.replace(comm, '');
solution = stringWithoutComment;
});
return solution;
}