1
var removeComments = function (source) {
2
let result = [];
3
let multi_line_comment = false;
4
let str = "";
5

6
source.forEach((line) => {
7
for (let idx = 0; idx < line.length; ++idx) {
8
// if /* is not ongoing, check for start of a comment or not
9
if (!multi_line_comment) {
10
// if comment is //, ignore the rest of the line
11
if (line[idx] + line[idx + 1] === "//") {
12
break;
13
}
14
// if comment if /*, set multi-line flag and move to next index
15
else if (line[idx] + line[idx + 1] === "/*") {
16
multi_line_comment = true;
17
++idx;
18
}
19
// if not a comment start, add to string to be added to result
20
else {
21
str += line[idx];
22
}
23
}
24
// if /* comment is ongoing
25
else {
26
// if closing comment */ is encountered, set multi-line flag off and move to next index
27
if (line[idx] + line[idx + 1] === "*/") {
28
multi_line_comment = false;
29
++idx;
30
}
31
}
32
}
33
// if /* comment is not ongoing and str is not empty, add to result as one code line
34
if (str.length && !multi_line_comment) {
35
result.push(str);
36
str = ""; //reset the str
37
}
38
});
39
return result;
40
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0