Skip to content

Instantly share code, notes, and snippets.

@jabczyk
Created February 14, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabczyk/eace9323a299f2499cf2d7dd0d2b67cb to your computer and use it in GitHub Desktop.
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 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