Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>code poetry</title>
- <style>
- body {background-color: powderblue;}
- h1 {color: blue;}
- p {color: red;}
- </style>
- <script language="javascript">
- var archive = [];
- var commentary = []; // ["dog", "cat", "purple pickle"];
- // shuffle function copied
- // from http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
- function shuffle(array) {
- var currentIndex = array.length, temporaryValue, randomIndex;
- // While there remain elements to shuffle...
- while (0 !== currentIndex) {
- // Pick a remaining element...
- randomIndex = Math.floor(Math.random() * currentIndex);
- currentIndex -= 1;
- // And swap it with the current element.
- temporaryValue = array[currentIndex];
- array[currentIndex] = array[randomIndex];
- array[randomIndex] = temporaryValue;
- }
- return array;
- }
- // END OF BORROWED CODE SECTION
- // print an array element to the mainlist DOM object
- function printArrayElements(element, index, array) {
- var stagelist = document.getElementById('mainlist'); // this should be a <ul> element
- if(stagelist) {
- var node = document.createElement("LI"); // Create a <li> node
- var textnode = document.createTextNode(element); // Create a text node
- node.appendChild(textnode); // Append the text to
- stagelist.appendChild(node);
- }
- else {
- console.log("Make sure the mainlist div is defined before calling this function");
- }
- }
- // shuffle and re-display the list of answers
- function shakeItUp() {
- console.log("shaking it up");
- var stagelist = document.getElementById('mainlist');
- // randomize the commentary array
- shuffle(commentary);
- // clear out stagelist by setting it's html value to an empty string
- stagelist.innerHTML = '';
- // print out list again
- commentary.forEach(printArrayElements);
- }
- // archive each submission into web storage
- function archiveCommentary(input) {
- if(localStorage.commentarchive) {
- localStorage.commentarchive = localStorage.commentarchive + "\n" + input;
- }
- else {
- localStorage.commentarchive = input;
- }
- //archive.push(input);
- }
- function getArchiveCommentary() {
- }
- // prompt the user to enter a new array item
- function promptUser() {
- var question = "Which crater would you colonize on the moon?";
- var input = window.prompt(question,":)");
- if(input != null && input != "") {
- // append their answer to the commentary array
- commentary.push(input);
- archiveCommentary(input);
- }
- // re-shuffle their answer into the mix
- shakeItUp();
- }
- </script>
- </head>
- <body>
- <h1>What did you say?</h1>
- <div id="mainstage"><ul id="mainlist"></ul></div>
- <script language="javascript">
- shakeItUp();
- </script>
- <button onclick="promptUser()">prompt user</button>
- <button onclick="shakeItUp()">shake it up</button>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement