Skip to content

Commit db765b4

Browse files
committed
solving strip comments
1 parent 02e70f9 commit db765b4

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

strip_comments/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function solution(text, markers) {
2+
if(!isMarker(text, markers)) return text;
3+
const array = text.split('');
4+
let result = '';
5+
6+
for(let i = 0; i < array.length; i++) {
7+
if(isMarker(array[i], markers)) {
8+
result = trimEnd(result);
9+
const lineBreakIndex = findNextLineBreak(array.slice(i));
10+
if(lineBreakIndex === -1) {
11+
array.splice(i, (array.length - 1) - i);
12+
} else {
13+
array.splice(i, lineBreakIndex - 1);
14+
}
15+
} else {
16+
result += array[i];
17+
}
18+
}
19+
return trimEnd(result);
20+
}
21+
22+
function isMarker(text, markers) {
23+
return markers.includes(text);
24+
}
25+
26+
function trimEnd(text) {
27+
return text.lastIndexOf(' ') === text.length - 1 ? text.trimEnd() : text;
28+
}
29+
30+
function findNextLineBreak(text) {
31+
return text.indexOf('\n');
32+
}
33+
34+
module.exports = { solution, isMarker, findNextLineBreak };

strip_comments/main.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const { isMarker, findNextLineBreak, solution } = require('./main');
2+
3+
describe('Check marker test', () => {
4+
test('a is not a marker of #', () => {
5+
expect(isMarker('a', ['#'])).toBe(false);
6+
});
7+
8+
test('# is a marker of #', () => {
9+
expect(isMarker('#', ['#'])).toBe(true);
10+
});
11+
12+
test('b is a not marker of #,!', () => {
13+
expect(isMarker('b', ['#', '!'])).toBe(false);
14+
});
15+
16+
test('! is a marker of #,!', () => {
17+
expect(isMarker('!', ['#', '!'])).toBe(true);
18+
});
19+
});
20+
21+
describe('find the next index of \n', () => {
22+
test('the next index of abc\n is 3', () => {
23+
expect(findNextLineBreak('abc\n')).toBe(3);
24+
});
25+
26+
test('the next index of abc is -1', () => {
27+
expect(findNextLineBreak('abc')).toBe(-1);
28+
});
29+
30+
test('the next index of abcde\nabcde\n is 5', () => {
31+
expect(findNextLineBreak('abcde\nabcde\n')).toBe(5);
32+
});
33+
});
34+
35+
describe('Strip comments', () => {
36+
test('a returns a', () => {
37+
expect(solution('a', ['#'])).toBe('a');
38+
});
39+
40+
test('\n returns \n', () => {
41+
expect(solution('\n', ['#'])).toBe('\n');
42+
});
43+
44+
test('a\n returns a\n', () => {
45+
expect(solution('a\n', ['#'])).toBe('a\n');
46+
});
47+
48+
test('a# returns a', () => {
49+
expect(solution('a#', ['#'])).toBe('a');
50+
});
51+
52+
test('a#hello returns a', () => {
53+
expect(solution('a#hello', ['#'])).toBe('a');
54+
});
55+
56+
test('#hello returns ', () => {
57+
expect(solution('#hello', ['#'])).toBe('');
58+
});
59+
60+
test('a#hello\nh returns a\nh', () => {
61+
expect(solution('a#hello\nh', ['#'])).toBe('a\nh');
62+
});
63+
64+
test('a#hello\nhello\n#hi returns ahello', () => {
65+
expect(solution('a#hello\nhello\n#hi', ['#'])).toBe('a\nhello\n');
66+
});
67+
68+
test('aa bb cc returns ahello', () => {
69+
expect(solution('aa bb cc ', ['#'])).toBe('aa bb cc');
70+
});
71+
72+
test('aa bb # cc returns aa bb', () => {
73+
expect(solution('aa bb # cc', ['#'])).toBe('aa bb');
74+
});
75+
76+
test('aa# bb cc returns aa', () => {
77+
expect(solution('aa# bb cc', ['#'])).toBe('aa');
78+
});
79+
80+
test('aa # bb\ncc dd return aa\ncc dd', () => {
81+
expect(solution('aa # bb\ncc dd', ['#'])).toBe('aa\ncc dd');
82+
});
83+
84+
test('no markers return the original text', () => {
85+
expect(solution('aa # bb # cc', [])).toBe('aa # bb # cc');
86+
});
87+
88+
test('apples, pears # and bananas\n returns apples, pears\n', () => {
89+
expect(solution('apples, pears # and bananas\n', ['#','!'])).toBe('apples, pears\n');
90+
});
91+
92+
test('aa # bb # cc returns aa', () => {
93+
expect(solution('aa # bb # cc', ['#','!'])).toBe('aa');
94+
});
95+
96+
test('apples, pears # and bananas\ngrapes\nbananas !apples returns apples, pears\ngrapes\nbananas', () => {
97+
expect(solution('apples, pears # and bananas\ngrapes\nbananas !apples', ['#','!'])).toBe('apples, pears\ngrapes\nbananas');
98+
});
99+
});

0 commit comments

Comments
 (0)