Advertisement
darkenvy

Reno McKenzie

Feb 23rd, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Lorem Ipsum Generator
  2.  
  3.    Lorem Ipsum filler text is often used to mock how a page might look with real-looking text.
  4.  
  5.    Your task is to create a program that generates filler text like you can see that our program
  6.    produced by changing the function "generateIpsum()" below.
  7.    
  8.    The source for the filler words must come from the variable "words" to produce about 1000
  9.    characters of output.
  10.    
  11.    The time limit for this task is 20 minutes. If you run out of time, it is sufficient to
  12.    explain how your solution is better or worse than the output of our program and how you
  13.    would improve it if you had more time.
  14.    
  15.    Send the contents of this pane and the contents of the Result pane as text files when done.
  16.    
  17. */
  18.  
  19. var words = "The sky above the port was the color of television, tuned to a dead channel. All this happened, more or less. I had the story, bit by bit, from various people, and, as generally happens in such cases, each time it was a different story. It was a pleasure to burn.";
  20.  
  21. var newWords = words.match(/(\w+)/g);
  22.  
  23. // Cheap-o probability distribution.
  24. // https://plus.maths.org/content/mystery-zipf
  25. var randWordLength = [
  26.   10,9,8,7,
  27.   6,6,
  28.   5,5,
  29.   4,4,
  30.   3,3,3,3,
  31.   2,2,2,
  32.   1,1,1
  33. ];
  34.  
  35. // https://strainindex.wordpress.com/2008/07/28/the-average-sentence-length/
  36. var randomSentenceLength = [
  37.   25,
  38.   21,21,
  39.   17,17,17,
  40.   14,14,
  41.   11
  42. ];
  43.  
  44. // Probability Function
  45. function probFunc(lengthList) {
  46.   return lengthList[Math.floor(Math.random() * lengthList.length)];
  47. }
  48.  
  49. function shuffleArray(array) {
  50.     for (var i = array.length - 1; i > 0; i--) {
  51.         var j = Math.floor(Math.random() * (i + 1));
  52.         var temp = array[i];
  53.         array[i] = array[j];
  54.         array[j] = temp;
  55.     }
  56.     return array;
  57. }
  58.  
  59. function findWordOfLength(wordList, len) {
  60.   var found = '';
  61.   wordList = shuffleArray(wordList);
  62.   wordList.forEach(function(word) {
  63.     if (word.length === len) {found = word;}
  64.   })
  65.   return found;
  66. }
  67.  
  68. function createSentence() {
  69.   var sentence = '';
  70.   var currLength = 0;
  71.   var maxLength = probFunc(randomSentenceLength);
  72.   while (currLength < maxLength) {
  73.     // sentence += ' ' + newWords[probFunc(randWordLength)];
  74.     sentence += ' ' + findWordOfLength(newWords, probFunc(randWordLength));
  75.     currLength += 1;
  76.   }
  77.   if ( parseInt(Math.random()*3) === 1) {
  78.     return sentence + ',';
  79.   } else {
  80.     return sentence + '.';
  81.   }
  82. }
  83.  
  84. function generateIpsum() {
  85.   var sentence = '';
  86.   while (sentence.length + 50 < 1000) {
  87.     sentence += createSentence();
  88.   }
  89.   // Uppercase sentences
  90.   sentence = sentence.replace(/(?:\.\s\w)|(?:^\s?\w)/g, function(m1) {
  91.     return m1.toUpperCase()
  92.   });
  93.  
  94.   return sentence;
  95.     // return words; // TODO: Replace this with code that generates Ipsum style filler text from "words"
  96. }
  97.  
  98. document.getElementById('fill').innerHTML = generateIpsum();
  99. // console.log(generateIpsum())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement