Advertisement
Guest User

Untitled

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