-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstripComments.js
More file actions
27 lines (25 loc) · 1.04 KB
/
stripComments.js
File metadata and controls
27 lines (25 loc) · 1.04 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
function solution(input, markers) {
// make array from string seperating at \n
let lineByLine = input.split('\n')
// make empty array to return later
let minsString = []
// function to return sliced value from 0 up to the marker character
const markerChop = (marker, sentence) => {
return sentence.slice(0, sentence.indexOf(marker)).trim()
}
// go through lineByLine
lineByLine.forEach(sentence => {
// if sentence doesn't contain either maker return push sentence to minsString
if (!sentence.includes(markers[0]) && !sentence.includes(markers[1])) {
minsString.push(sentence)
} else {
// check which marker the string has and remove it + everything after.
markers.forEach(marker => {
if (sentence.includes(marker)) minsString.push(markerChop(marker, sentence))
})
}
})
// return minsString joined back together with \n
return minsString.join('\n')
}
solution("apples, plums % and bananas\npears\noranges !applesauce", ["%", "!"])