Advertisement
Guest User

Untitled

a guest
Dec 15th, 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.             if (parameter[i].length == 1)
  32.                 parameter[i] = '<span class="blue">' + parameter[i] + '</span>'
  33.             element.innerHTML += parameter[i];
  34.         }
  35.  
  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 the char at index i is the same as the value of the array and the value of the array is not equal to the char at index 0
  134.                     // then go on.
  135.                     if (wordCopy.charAt(j) === userGuess[i] && userGuess[i] != word.charAt(0)) {
  136.                         userGuess[i] = '<span class="yellow">' + input.value.charAt(i) + '</span>'
  137.                        
  138.                         // Splitting the wordCopy.
  139.                         var wordCopySplit = wordCopy.split("");
  140.  
  141.                         // Setting the index to nothin.
  142.                         wordCopySplit[j] = '';
  143.                         // Add all elements of the array.
  144.                         wordCopySplit.join("");
  145.  
  146.                         wordCopy = '';
  147.                        
  148.                         for (var k = 0; k < wordCopySplit.length; k++)
  149.                             wordCopy += wordCopySplit[k]
  150.                            
  151.                         continue;
  152.                     }
  153.                 }          
  154.             }
  155.         }
  156.  
  157.         addUserGuess(userGuess)
  158.         console.log(userGuess)
  159.         input.value = ''
  160.     }
  161.  
  162.     function log (date, string) {
  163.         console.log('[' + date.getHours() + ':' + date.getMinutes() + ":" + date.getSeconds() + '] ' + string)      
  164.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement