Guest User

Untitled

a guest
Mar 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <html>
  2. <body>
  3. <pre>1---------10--------20--------30--------40--------50--------</pre>
  4. </body>
  5. <script>
  6.  
  7. var text = "NICHOLAS PEROT SMITH WELCH & SMITH and SUZIE MCFAIRLAIN and ROBERT MCFAIRLAIN MRS. LOREM IPSUM and WORSHESTERSHIRE and OBNOXIOUS FLAVANOIDS and MARRY HAD A LITTLE LAMB AND IT SQUEAKED TWICE";
  8.  
  9. var body = document.getElementsByTagName("body")[0];
  10. var child = document.createElement("pre");
  11. child.innerHTML = wordWrap(text, 60, 3)
  12. body.appendChild(child);
  13.  
  14. function wordWrap(sentence, cols, rows) {
  15. var words = sentence.split(/\s+/);
  16. var lines = [];
  17. var currentword = 0;
  18. var space = " ";
  19. for (var i = 0; i < rows; i++) {
  20. lines[i] = "";
  21. while(currentword < words.length) {
  22. if ((lines[i] + space + words[currentword]).length <= cols) {
  23. lines[i] += (!lines[i].length ? "" : space) + words[currentword++];
  24. } else {
  25. break;
  26. }
  27. }
  28. }
  29.  
  30. if (currentword < words.length) {
  31. var missingwords = "";
  32. while(currentword < words.length) {
  33. missingwords += words[currentword++];
  34. }
  35. alert("TOO LONG! MISSING:" + missingwords);
  36. }
  37.  
  38. return lines.join("\r\n");
  39. }
  40.  
  41. </script>
  42. </html>
Add Comment
Please, Sign In to add comment