Created
February 14, 2020 16:52
-
-
Save jabczyk/eace9323a299f2499cf2d7dd0d2b67cb to your computer and use it in GitHub Desktop.
/r/shittyprogramming challenge: add a new line after the end of each sentence, the shittiest way possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* this is a paragraph parser service class */ | |
class ParagraphParserService { | |
constructor () { | |
this.n = ` | |
` | |
this.line = ` | |
` | |
} | |
addNewLineAfterSentence ({ paragraphOfText }) { | |
let x = paragraphOfText.split(''), | |
y = [] | |
x.forEach((a, i) => { | |
if (a === ' ') { | |
if (x[i - 1] === '.') { | |
// update variable | |
y = [...y, i] | |
if (x[i + 1] === this.line) y.pop() | |
} | |
if (x[i - 1] === '?') { | |
// update variable | |
y = [...y, i] | |
if (x[i + 1] === this.line) y.pop() | |
} | |
} | |
}) | |
y = y.map((z, i) => { | |
x = [...x.slice(0, z + i + 1), this.n, ...x.slice(z + i + 1, 99999999999)] | |
return 'new line added' | |
}) | |
if ([...new Set(y)].length !== 1 || [...new Set(y)][0] !== 'new line added') | |
throw new Error('Failed to add new lines') | |
x = x.join('') | |
// return the result | |
const result = x | |
return result | |
} | |
} | |
const sentence = 'This. is a text... test. \ntesting? text 7:00 p.m.' | |
console.log( | |
new ParagraphParserService().addNewLineAfterSentence({ | |
paragraphOfText: sentence | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment