daily pastebin goal
51%
SHARE
TWEET

Uncaught TypeError: Cannot read property 'style' of null

a guest Jul 12th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. GAME RULES:
  3.  
  4. - The game has 2 players, playing in rounds
  5. - In each turn, a player rolls a dice as many times as he whishes. Each result get added to his ROUND score
  6. - BUT, if the player rolls a 1, all his ROUND score gets lost. After that, it's the next player's turn
  7. - The player can choose to 'Hold', which means that his ROUND score gets added to his GLBAL score. After that, it's the next player's turn
  8. - The first player to reach 100 points on GLOBAL score wins the game
  9.  
  10. */
  11. var scores, roundScore, activePlayer, gamePlaying;
  12.  
  13. init();
  14.  
  15. //document.querySelector('#current-' + activePlayer).textContent = dice; // SETTER
  16. //document.querySelector('#current-' + activePlayer).innerHTML = '<em>' + dice + '</em>';
  17.  
  18. //var x = document.querySelector('#score-0').textContent; // GETTER
  19. //console.log(x);
  20.  
  21.  
  22.  
  23. // Events: Notifications that are sent to notify the code that something happened
  24. // on the webpage;
  25. // Examples: clicking a button, resizing a window, scrolling down, or pressing
  26. // a key.
  27. // Event listener: a function that performs an action based on a certain event.
  28. // It waits for a specific event to happen.
  29.  
  30. //var lastDice;
  31. document.querySelector('.btn-roll').addEventListener('click', function() {
  32.     if(gamePlaying) {
  33.  
  34.         // 1. Random number
  35.         var dice1 = Math.floor(Math.random() * 6) + 1;
  36.         var dice2 = Math.floor(Math.random() * 6) + 1;
  37.         //Check for double 6
  38.    
  39.        
  40.        
  41.        
  42.        
  43.         // 2. Display the result
  44.         document.getElementById('dice-1').style.display = 'block';
  45.         document.getElementById('dice-2').style.display = 'block';
  46.        
  47.         document.getElementById('dice-1').src = 'dice-' + dice1 + '.png';
  48.         document.getElementById('dice-1').src = 'dice-' + dice2 + '.png';
  49.  
  50.         // 3. Update the round score IF the rolled number was NOT 1
  51.         if (dice1 !== 1 && dice2 !== 1) {
  52.             //Add score
  53.             roundScore += dice1 + dice2;
  54.             document.querySelector('#current-' + activePlayer).textContent = roundScore;
  55.         } else {
  56.             //Next player
  57.             nextPlayer();
  58.         }
  59.  
  60.         /*
  61.         if (dice === 6 && lastDice === 6) {
  62.             scores[activePlayer] = 0;
  63.             document.querySelector('#score-' + activePlayer).textContent = '0';
  64.             nextPlayer();
  65.         } else if (dice > 1) {
  66.             //Add score
  67.             roundScore += dice;
  68.             document.querySelector('#current-' + activePlayer).textContent = roundScore;
  69.         } else {
  70.             //Next player
  71.             nextPlayer();
  72.         }
  73.         lastDice = dice;
  74.         */
  75.        
  76.     }
  77. });
  78.  
  79. // Implementing Our 'Hold' Function and the DRY Principle
  80. document.querySelector('.btn-hold').addEventListener('click', function() {
  81.     if(gamePlaying) {
  82.         // Add current score to global score
  83.         scores[activePlayer] += roundScore;
  84.         // Update the UI
  85.         document.querySelector('#score-' + activePlayer).textContent =
  86.         scores[activePlayer];
  87.  
  88.         var input = document.querySelector('.final-score').value;
  89.         var winningScore;
  90.  
  91.         // Undefined, 0, null, or "" are COERCED to false. Anything else is true.
  92.         if(input) {
  93.             var winningScore = input;
  94.         } else {
  95.             winningScore = 100;
  96.         }
  97.    
  98.         // Check if player won the game
  99.         if (scores[activePlayer] >= winningScore) {
  100.             gamePlaying = false;
  101.             document.querySelector('#name-' + activePlayer).textContent =
  102.             'WINNER!';
  103.             document.getElementById('dice-1').style.display = 'none';
  104.             document.getElementById('dice-2').style.display = 'none';
  105.             document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
  106.             document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
  107.        
  108.         } else {
  109.             nextPlayer();
  110.     }
  111.     }
  112.    
  113. });
  114.  
  115. function nextPlayer() {
  116.     activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
  117.     roundScore = 0;
  118.     document.getElementById('current-0').textContent = '0';
  119.     document.getElementById('current-1').textContent = '0';
  120.  
  121.     document.querySelector('.player-0-panel').classList.toggle('active');
  122.     document.querySelector('.player-1-panel').classList.toggle('active');
  123.  
  124.     document.getElementById('dice-1').style.display = 'none';
  125.     document.getElementById('dice-2').style.display = 'none';  
  126. }
  127.  
  128. // Creating A Game Initialization Function
  129. document.querySelector('.btn-new').addEventListener('click', init);
  130.  
  131. function init() {
  132.     scores = [0, 0];
  133.     roundScore = 0;
  134.     activePlayer = 0;
  135.     gamePlaying = true;
  136.     document.getElementById('dice-1').style.display = 'none';
  137.     document.getElementById('dice-2').style.display = 'none';
  138.     document.getElementById('score-0').textContent = '0';
  139.     document.getElementById('score-1').textContent = '0';
  140.     document.getElementById('current-0').textContent = '0';
  141.     document.getElementById('current-1').textContent = '0';
  142.  
  143.     document.getElementById('name-0').textContent = 'Player 1';
  144.     document.getElementById('name-1').textContent = 'Player 2';
  145.     document.querySelector('.player-0-panel').classList.remove('winner');
  146.     document.querySelector('.player-1-panel').classList.remove('winner');
  147.     document.querySelector('.player-0-panel').classList.remove('active');
  148.     document.querySelector('.player-1-panel').classList.remove('active');
  149.     document.querySelector('.player-0-panel').classList.add('active');
  150. }
  151.  
  152. // Finishing Touches
  153.  
  154. // Coding Challenge 6
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top