Skip to content

Instantly share code, notes, and snippets.

@robertcoopercode
Created November 12, 2017 19:53
Show Gist options
  • Save robertcoopercode/21d86162c6276347cafb12d2fbd25e47 to your computer and use it in GitHub Desktop.
Save robertcoopercode/21d86162c6276347cafb12d2fbd25e47 to your computer and use it in GitHub Desktop.
// Code that generates the random lorem ipsum text
// Create a new object called loremIpsum by invoking the GenerateNewText constructor function
const loremIpsum = new GenerateNewText();
// Constructor function that creates an object with the sentences property
function GenerateNewText() {
// Add property to the object
this.sentences =
[
"Awesome quote number 1.",
"Second awesome quote.",
"Yet another great quote.",
"Ok, last quote."
];
}
// Method to the GenerateNewText constructor function that generates a random sentence
GenerateNewText.prototype.getRandomSentence = function() {
let randomSentence = this.sentences[Math.floor(Math.random() * this.sentences.length)]
return randomSentence;
}
// Method to the GenerateNewText constructor function that generates a paragraph from random sentences
GenerateNewText.prototype.getParagraph = function() {
let paragraph = "";
// Set the minimum number of words
let minimumCharacterLength = 250;
let firstSentence = true;
while (paragraph.length < minimumCharacterLength) {
if (firstSentence) {
paragraph = paragraph.concat(this.getRandomSentence());
firstSentence = false;
} else {
paragraph = paragraph.concat(" " + this.getRandomSentence());
}
}
return paragraph;
}
// Method to the GenerateNewText constructor function that gerates multiple paragraphs from paragraphs
GenerateNewText.prototype.getAllParagraphs = function(numberOfParagraphs) {
let allParagraphs = [];
// Generate the number of paragraphs as specified by the user
while (allParagraphs.length < numberOfParagraphs) {
allParagraphs.push(this.getParagraph());
}
// Convert array into HTML string
let paragraphHTML = "";
allParagraphs.forEach(function (paragraph) {
paragraphHTML += "<p>" + paragraph + "</p>";
});
return paragraphHTML;
}
module.exports = loremIpsum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment