Advertisement
Guest User

Poetry project

a guest
Jul 28th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.31 KB | None | 0 0
  1. <html>
  2.         <head>
  3.                 <title>code poetry</title>
  4.  
  5.                 <style>
  6.                         body {background-color: powderblue;}
  7.                         h1   {color: blue;}
  8.                         p    {color: red;}
  9.  
  10.                 </style>
  11.  
  12.                 <script language="javascript">
  13.                         var archive = [];
  14.                         var commentary = []; // ["dog", "cat", "purple pickle"];
  15.  
  16.                         // shuffle function copied
  17.                         // from http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
  18.                         function shuffle(array) {
  19.                             var currentIndex = array.length, temporaryValue, randomIndex;
  20.  
  21.                             // While there remain elements to shuffle...
  22.                             while (0 !== currentIndex) {
  23.  
  24.                                 // Pick a remaining element...
  25.                                 randomIndex = Math.floor(Math.random() * currentIndex);
  26.                                 currentIndex -= 1;
  27.  
  28.                                 // And swap it with the current element.
  29.                                 temporaryValue = array[currentIndex];
  30.                                 array[currentIndex] = array[randomIndex];
  31.                                 array[randomIndex] = temporaryValue;
  32.                             }
  33.  
  34.                             return array;
  35.                         }
  36.                         // END OF BORROWED CODE SECTION
  37.  
  38.                         // print an array element to the mainlist DOM object
  39.                         function printArrayElements(element, index, array) {
  40.                             var stagelist = document.getElementById('mainlist'); // this should be a <ul> element
  41.                             if(stagelist) {
  42.                                 var node = document.createElement("LI");                 // Create a <li> node
  43.                                 var textnode = document.createTextNode(element);         // Create a text node
  44.                                 node.appendChild(textnode);                              // Append the text to
  45.                                 stagelist.appendChild(node);
  46.                             }
  47.                             else {
  48.                                 console.log("Make sure the mainlist div is defined before calling this function");
  49.                             }
  50.                         }
  51.  
  52.                         // shuffle and re-display the list of answers
  53.                         function shakeItUp() {
  54.                             console.log("shaking it up");
  55.                             var stagelist = document.getElementById('mainlist');
  56.  
  57.                             // randomize the commentary array
  58.                             shuffle(commentary);
  59.  
  60.                             // clear out stagelist by setting it's html value to an empty string
  61.                             stagelist.innerHTML = '';
  62.                            
  63.                             // print out list again
  64.                             commentary.forEach(printArrayElements);
  65.                         }
  66.  
  67.                         // archive each submission into web storage
  68.                         function archiveCommentary(input) {
  69.                             if(localStorage.commentarchive) {
  70.                                 localStorage.commentarchive = localStorage.commentarchive + "\n" + input;
  71.                             }
  72.                             else {
  73.                                 localStorage.commentarchive = input;
  74.                             }
  75.  
  76.                             //archive.push(input);
  77.                         }
  78.  
  79.                         function getArchiveCommentary() {
  80.                         }
  81.  
  82.                         // prompt the user to enter a new array item
  83.                         function promptUser() {
  84.                             var question = "Which crater would you colonize on the moon?";
  85.                             var input = window.prompt(question,":)");
  86.                             if(input != null && input != "") {
  87.                                 // append their answer to the commentary array
  88.                                 commentary.push(input);
  89.                                 archiveCommentary(input);
  90.                             }
  91.  
  92.                             // re-shuffle their answer into the mix
  93.                             shakeItUp();
  94.                         }
  95.                 </script>
  96.         </head>
  97.         <body>
  98.                 <h1>What did you say?</h1>
  99.                 <div id="mainstage"><ul id="mainlist"></ul></div>
  100.  
  101.                 <script language="javascript">
  102.                         shakeItUp();
  103.                 </script>
  104.                 <button onclick="promptUser()">prompt user</button>
  105.                 <button onclick="shakeItUp()">shake it up</button>
  106.  
  107.         </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement