mochination

phrase chooser

Sep 2nd, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.  
  3.     var randomNoun = {
  4.         new: "",
  5.         old: ""
  6.     };
  7.     var randomVerb = {
  8.         new: "",
  9.         old: ""
  10.     }
  11.     var finalPhrase = {
  12.         new: "",
  13.         old: ""
  14.     };
  15.  
  16.     //Get either a random phrase from verbs and nouns or have a small chance of getting a set phrase
  17.     function getRandomPhrase() {
  18.  
  19.         //Low chance set phrases
  20.         var rarePhrase = ["stops being a dog", "stops being a frog", "gets off the internet", "ランダムに日本語を使うのを止められたら", "cuts some weight", "stops talking in the third person", "wakes up", "forgets about his one-sided love", "fights you irl", "makes gymnopedies", "folds 1000 cranes"];
  21.  
  22.         //Array of verbs for random phrases
  23.         var verbs = ["finds", "plays", "sees", "becomes", "watches", "falls in love with", "realizes", "discovers", "passes", "stops being", "takes", "meets", "gets", "obtains", "accepts", "adopts", "builds", "avoids", "drops", "chases", "inhales", "obeys", "visits", "passes", "notices", "reaches"];
  24.  
  25.         //Array of nouns for random phrases
  26.         var nouns = ["the pasta on the piano", "the sunset", "rainfall", "the sunrise", "nightfall", "a dog", "a frog", "a fish", "those moments lost in time", "tears in rain", "a real man", "a torrential downpour", "ferrets", "slipknot paranoia", "faster-than-light travel", "a flicker of light", "singularity", "the event horizon", "the ceiling fan", "photosynthesis", "a bear", "sea coral", "some birds", "the beat", "the groove", "daikon", "anime", "...true love"];
  27.  
  28.         //Rarer nouns
  29.         var rareNouns = ["ships off the shoulder of Orion", "C-beams glittering in the dark near Tannhäuser Gate", "ZALGO", "the dog that he is, or was Jonathan actually the fish? Maybe he was the frog"]
  30.  
  31.         //Function that chooses random element from array
  32.         function chooseRandomFromArray(arrayName) {
  33.             return (arrayName[Math.floor(Math.random() * arrayName.length)]);
  34.         };
  35.  
  36.         //Get a random noun, potentially getting a rare noun
  37.         if (Math.random() > 0.95) {
  38.             randomNoun.new = chooseRandomFromArray(rareNouns);
  39.         } else {
  40.             randomNoun.new = chooseRandomFromArray(nouns);
  41.         }
  42.         //Get a random verb
  43.         randomVerb.new = chooseRandomFromArray(verbs);
  44.         randomPhrase = randomVerb.new + " " + randomNoun.new;
  45.  
  46.         //Either roll the random phrase or a set phrase
  47.         if (Math.random() > 0.9) {
  48.             finalPhrase.new = chooseRandomFromArray(rarePhrase);
  49.         } else {
  50.             finalPhrase.new = randomPhrase;
  51.         }
  52.  
  53.         //Make sure that the last noun and verb are not being used again
  54.         if (randomNoun.new === randomNoun.old || randomVerb.old === randomVerb.new || finalPhrase.old === finalPhrase.new) {
  55.             getRandomPhrase();
  56.         }
  57.  
  58.         //Assign "old" values so no two words are used twice in a row
  59.         randomNoun.old = randomNoun.new;
  60.         randomVerb.old = randomVerb.new;
  61.         finalPhrase.old = finalPhrase.new;
  62.  
  63.     } //getRandomPhrase
  64.  
  65.     //Initially get random phrase and display it on the page
  66.     getRandomPhrase();
  67.     $('#phrase').text(finalPhrase.new);
  68.  
  69.     //On click, get the random phrase and display it on the page
  70.     $('#phrase').click(function () {
  71.         getRandomPhrase();
  72.         $('#phrase').text(finalPhrase.new);
  73.     });
  74.  
  75. });
Advertisement
Add Comment
Please, Sign In to add comment