Sichanov

07. Format the Text

Mar 25th, 2023
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let textArea = document.getElementById('input');
  3.     let text = textArea.value;
  4.     let splitText = text.split('.');
  5.     let outputTextList = [];
  6.     let outputText = '';
  7.     let counter = 0;
  8.     for (let i = 0; i < splitText.length; i++) {
  9.         if (splitText[i].length >= 1) {
  10.             counter += 1;
  11.             outputText += splitText[i].trimStart() + '.';
  12.             if (counter === 3 || i === splitText.length - 1) {
  13.                 outputTextList.push(outputText);
  14.                 counter = 0;
  15.                 outputText = '';
  16.             }
  17.         }
  18.     }
  19.     if (outputText) {
  20.         outputTextList.push(outputText);
  21.     }
  22.     let output = document.getElementById('output');
  23.     for (const el of outputTextList) {
  24.         let z = document.createElement("p");
  25.         z.innerHTML = el;
  26.         output.appendChild(z);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment