Skip to content

Instantly share code, notes, and snippets.

@aziis98
Created May 2, 2021 00:15
Show Gist options
  • Select an option

  • Save aziis98/07a19a0d04f0b2e024f6895994df2642 to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/07a19a0d04f0b2e024f6895994df2642 to your computer and use it in GitHub Desktop.
Small snippet of JS to generate some random looking text instead of using lorem ipsum.

Random-Looking Text

Small snippet of JS to generate some random looking text instead of using lorem ipsum.

Usage

In HTML put some <span class="random-text"></span>

const letters = {
vowels: 'aeiou'.split(''),
consonants: 'bcdfghlmnpqrstvz'.split(''),
};
function randomElement(list) {
return list[Math.floor(Math.random() * list.length)];
}
function randomSyl() {
return randomElement(letters.consonants) + randomElement(letters.vowels);
}
function randomWord(minSylLen = 1, maxSylLen = 4) {
const randomSylLen = Math.floor(Math.random() * (1 + maxSylLen - minSylLen)) + minSylLen;
return Array(randomSylLen).fill(0).map(() => randomSyl()).join('');
}
function randomPhrase(minWords = 5, maxWords = 50) {
const randomWordCount = Math.floor(Math.random() * (1 + maxWords - minWords)) + minWords;
const phrase = Array(randomWordCount).fill(0).map(() => randomWord()).join(' ');
return phrase[0].toUpperCase() + phrase.slice(1);
}
document.querySelectorAll('span.random-text').forEach($el => {
$el.innerText = randomPhrase();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment