Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     // Amount of times the user has attempted to guess the word.
  3.     var attempts = 0;
  4.     // The actual word that will be generated.
  5.     var word = '';
  6.  
  7.     // Our submit element.
  8.     const submit = document.getElementById('submit');
  9.     // Our input element .
  10.     const input = document.getElementById('userInput');
  11.  
  12.     (function setup() {
  13.         generateRandomWord()
  14.         showFirstLetter()
  15.     })()
  16.  
  17.     // This function generates a random word from the array.
  18.     function generateRandomWord () {
  19.         // Get a random number from the length of the array to set as the generated word.
  20.         word = words[Math.floor(Math.random() * words.length)]
  21.         log(new Date(), word)
  22.     }
  23.  
  24.     function showFirstLetter () {
  25.         // Show the first letter of the generated word to the user.
  26.         document.getElementById('word').innerHTML = word.charAt(0);
  27.     }
  28.  
  29.     function addUserGuess(parameter) {
  30.         const parent = document.getElementsByTagName('ul')[0]
  31.         var element = document.createElement('li')
  32.  
  33.         // Add values from the array to the element.
  34.         for (var i = 0; i < parameter.length; i++)
  35.             element.innerHTML += parameter[i];
  36.  
  37.         // Give the element a class.
  38.         element.setAttribute('class', 'guess')
  39.         // Place the element in the browser
  40.         parent.appendChild(element)
  41.     }
  42.  
  43.     function resetPage () {
  44.         // Reset the word.
  45.         word = ''
  46.         // Reset the attempts.
  47.         attempts = 0
  48.         // Reset the input value.
  49.         input.value = ''
  50.  
  51.         // Get all elements which have the tag name 'li'
  52.         var elements = document.getElementsByTagName('li')
  53.         // While elements.length is true and the element we want to delete has the classname 'guess' at index 0.
  54.         while (elements.length && elements[0].classList[0] == 'guess')
  55.             // Remove the first element in the array.
  56.             elements[0].remove()
  57.  
  58.         // Toggle the page.
  59.         togglePage()
  60.     }
  61.  
  62.     function togglePage () {
  63.         var wrapper = document.getElementById('game_wrapper')
  64.         if (wrapper.style.display =='none')
  65.             wrapper.style.display = 'block'
  66.         else
  67.             wrapper.style.display = 'none'
  68.     }
  69.  
  70.     input.onkeypress = function() {
  71.         // Making sure the user can't insert numbers.
  72.         if ((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode > 122)
  73.             return false
  74.     }
  75.  
  76.     submit.onclick = function () {
  77.  
  78.         // Checking if there even is a word to guess, if not return.
  79.         if (!word.length)
  80.             return
  81.  
  82.         var userGuess = []
  83.  
  84.         // The length of the users guess has to be the same as the length of the generated word.
  85.         if (input.value.length != word.length) {
  86.             alert('The length of your guess must be ' + word.length + ' characters long.')
  87.             return
  88.         }
  89.  
  90.         // Increase the amount of attempts after we know for sure that they haven't guessed the word.
  91.         attempts++;
  92.         // Check if the amount of attempts is w5, if so they lost the game.
  93.         if (attempts == 5) {
  94.             alert('You have reached the maxmimum amount of attempts! The word was ' + word)
  95.             // Reset the page to start a new game.
  96.             resetPage()
  97.             return;
  98.         }
  99.  
  100.         // Now we know that the length of the users input is correct.
  101.         if (input.value == word) {
  102.             // User has won the game.
  103.             if (attempts > 1)
  104.                 alert('You have guessed the word within ' + attempts + ' attempts.')
  105.             else
  106.                 alert('You have guessed the word within ' + attempts + ' attempt.')
  107.            
  108.             resetPage()
  109.             return
  110.         }
  111.  
  112.         // Loop over the length of the generated word.
  113.         for (var i = 0; i < word.length; i++) {
  114.             // Check if the letter of the users input at index i is equal to the
  115.             // letter at index i of the generated word.
  116.             if (input.value.charAt(i) == word.charAt(i)) {
  117.                 // We now know the letter is at the right place in the word so we mark it red.
  118.                 userGuess.push('<span class="red">' + input.value.charAt(i) + '</span>')
  119.                 continue
  120.             }
  121.  
  122.             // The letter was not at the right place so we add it.
  123.             userGuess.push(input.value.charAt(i))
  124.         }
  125.  
  126.         // Loop over the length of the generated word.
  127.         var wordCopy = word;
  128.         for (var i = 0; i < userGuess.length; i++) {
  129.             // If the letter isn't on the right place it gets pushed into the userGuess array
  130.             // which means that we can check if the userGuess is equal to the words letter at index i
  131.             if (wordCopy.includes(userGuess[i])) {
  132.                 for (var j = 0; j < wordCopy.length; j++) {
  133.                     if (wordCopy.charAt(j) === userGuess[i] && userGuess[i] != word.charAt(0)) {
  134.                         userGuess[i] = '<span class="yellow">' + input.value.charAt(i) + '</span>'
  135.                        
  136.                         // Splitting the wordCopy.
  137.                         var wordCopySplit = wordCopy.split("");
  138.  
  139.                         // Setting the index to nothin.
  140.                         wordCopySplit[j] = '';
  141.                         // Add all elements of the array.
  142.                         wordCopySplit.join("");
  143.  
  144.                         wordCopy = '';
  145.                        
  146.                         for (var k = 0; k < wordCopySplit.length; k++)
  147.                             wordCopy += wordCopySplit[k]
  148.                            
  149.                         continue;
  150.                     }
  151.                 }          
  152.             }
  153.         }
  154.  
  155.         addUserGuess(userGuess)
  156.         input.value = ''
  157.     }
  158.  
  159.     function log (date, string) {
  160.         console.log('[' + date.getHours() + ':' + date.getMinutes() + ":" + date.getSeconds() + '] ' + string)      
  161.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement