-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy path4kyu_StripComments.js
38 lines (32 loc) · 1010 Bytes
/
4kyu_StripComments.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
38
// 4kyu - Strip Comments
// Description:
// 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(str, arr) {
str = str.split("\n")
for(let i = 0; i < str.length; i++){
let shorter;
for(let j = 0; j < arr.length; j++) {
let idx = str[i].indexOf(arr[j]);
if(idx > -1)
shorter = (idx<shorter||!shorter) ? idx : shorter;
}
if(shorter)
str[i] = str[i].substr(0, shorter).trim();
}
return str.join("\n");
}