Format Text

mail@pastecode.io avatar
unknown
javascript
7 months ago
797 B
1
Indexable
Never
function solve() {
  const sentences = getSentences()
  if (sentences.length === 0) { return }
  
  const outputContainer = document.getElementById('output')
  outputContainer.innerHTML = ''
  
  let paragraphText = ''
  sentences.forEach((sentence, idx) => {
    let oneBasedIndex = idx + 1;
    paragraphText += sentence

    if (oneBasedIndex % 3 === 0 || oneBasedIndex === sentences.length) {
      outputContainer.innerHTML += `<p>${paragraphText}</p>`; //works
      outputContainer.innerHTML += `<p> ${paragraphText} </p>`; //fails
      paragraphText = ''
    }
  });
  

  function getSentences() {
    const text = document.getElementById('input').value
    const sentences = text.split('.').filter(s => s.length > 0).map(s => `${s}.`)
    return sentences
  }
}
Leave a Comment