generate random sentences and paragraphs

This commit is contained in:
2025-01-09 23:16:38 -05:00
parent 0318070e94
commit fdf91a566b
+39 -4
View File
@@ -12,14 +12,49 @@ import dictionary from '../dictionary.txt?raw';
<script> <script>
const contentDiv = document.getElementById('content'); const contentDiv = document.getElementById('content');
let paragraph: HTMLParagraphElement;
function createParagraph() {
paragraph = document.createElement('p');
contentDiv.appendChild(paragraph);
}
createParagraph();
const dictionary = contentDiv.dataset.dictionary.split('\n'); const dictionary = contentDiv.dataset.dictionary.split('\n');
let wordCounter = 0;
let sentenceCounter = 0;
function addRandomString() { function addRandomString() {
const randomString = const startNewSentence =
Math.floor(Math.random() * wordCounter) >= 8 && wordCounter > 5;
if (startNewSentence) {
paragraph?.append('. ');
wordCounter = 0;
sentenceCounter += 1;
const startNewParagraph =
Math.floor(Math.random() * sentenceCounter) >= 3;
if (startNewParagraph) {
createParagraph();
sentenceCounter = 0;
}
}
let randomString =
dictionary[Math.floor(Math.random() * dictionary.length)]; dictionary[Math.floor(Math.random() * dictionary.length)];
const span = document.createElement('span');
span.textContent = randomString + ' '; if (wordCounter === 0) {
contentDiv?.appendChild(span); randomString = randomString[0].toUpperCase() + randomString.slice(1);
} else {
randomString = ' ' + randomString;
}
paragraph?.append(randomString);
wordCounter += 1;
} }
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {