Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var numSquares = 6;
  2. var colors = [];
  3. var pickedColor;
  4.  
  5. var squares = document.querySelectorAll(".heart");
  6.  
  7. // That's something I found on google but idk how it works
  8. var heartBefore = window.getComputedStyle(
  9.     document.querySelector('.heart'), ':before'
  10. );
  11. var heartAfter = window.getComputedStyle(
  12.     document.querySelector('.heart'), ':after'
  13. );
  14.  
  15. var colorDisplay = document.getElementById("colorDisplay");
  16. var messageDisplay = document.querySelector("#message");
  17. var h1 = document.querySelector("h1");
  18. var resetButton = document.querySelector("#reset");
  19. var modeButtons = document.querySelectorAll(".mode");
  20.  
  21.  
  22. init();
  23.  
  24. function init() {
  25.     setupModeButtons();
  26.     setupSquares();
  27.     reset();
  28. }
  29.  
  30. function setupModeButtons() {
  31.     for (var i = 0; i < modeButtons.length; i++) {
  32.         modeButtons[i].addEventListener("click", function () {
  33.             modeButtons[0].classList.remove("selected");
  34.             modeButtons[1].classList.remove("selected");
  35.             this.classList.add("selected");
  36.             this.textContent === "Easy" ? numSquares = 3 : numSquares = 6;
  37.             reset();
  38.         });
  39.     }
  40. }
  41.  
  42. function setupSquares() {
  43.     for (var i = 0; i < squares.length; i++) {
  44.         //add click listeners to squares
  45.         squares[i].addEventListener("click", function () {
  46.             //grab color of clicked square
  47.             var clickedColor = this.style.background;
  48.             //compare color to pickedColor
  49.             if (clickedColor === pickedColor) {
  50.                 messageDisplay.textContent = "Correct!";
  51.                 resetButton.textContent = "Play Again?";
  52.                 changeColors(clickedColor);
  53.                 h1.style.background = clickedColor;
  54.             } else {
  55.                 this.style.background = "#232323";
  56.                 messageDisplay.textContent = "Try Again";
  57.             }
  58.         });
  59.     }
  60. }
  61.  
  62.  
  63.  
  64. function reset() {
  65.     colors = generateRandomColors(numSquares);
  66.     //pick a new random color from array
  67.     pickedColor = pickColor();
  68.     //change colorDisplay to match picked Color
  69.     colorDisplay.textContent = pickedColor;
  70.     resetButton.textContent = "New Colors";
  71.     messageDisplay.textContent = "";
  72.     //change colors of squares
  73.     for (var i = 0; i < squares.length; i++) {
  74.         if (colors[i]) {
  75.             squares[i].style.display = "block";
  76.  
  77.             squares[i].style.background = colors[i];
  78.  
  79.         } else {
  80.             squares[i].style.display = "none";
  81.  
  82.         }
  83.     }
  84.     h1.style.background = "steelblue";
  85. }
  86.  
  87. resetButton.addEventListener("click", function () {
  88.     reset();
  89. })
  90.  
  91. function changeColors(color) {
  92.     //loop through all squares
  93.     for (var i = 0; i < squares.length; i++) {
  94.         //change each color to match given color
  95.         squares[i].style.background = color;
  96.     }
  97. }
  98.  
  99. function pickColor() {
  100.     var random = Math.floor(Math.random() * colors.length);
  101.     return colors[random];
  102. }
  103.  
  104. function generateRandomColors(num) {
  105.     //make an array
  106.     var arr = []
  107.     //repeat num times
  108.     for (var i = 0; i < num; i++) {
  109.         //get random color and push into arr
  110.         arr.push(randomColor())
  111.     }
  112.     //return that array
  113.     return arr;
  114. }
  115.  
  116. function randomColor() {
  117.     //pick a "red" from 0 - 255
  118.     var r = Math.floor(Math.random() * 256);
  119.     //pick a "green" from  0 -255
  120.     var g = Math.floor(Math.random() * 256);
  121.     //pick a "blue" from  0 -255
  122.     var b = Math.floor(Math.random() * 256);
  123.     return "rgb(" + r + ", " + g + ", " + b + ")";
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement