Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further abstraction #49

Open
freemvmt opened this issue Nov 15, 2019 · 2 comments
Open

Further abstraction #49

freemvmt opened this issue Nov 15, 2019 · 2 comments
Labels
compliment enhancement New feature or request

Comments

@freemvmt
Copy link

Dom.js file is really beautifully organised, kudos.

Had some quick ideas for making your code more DRY/further abstracting some of the repetition.

  1. Grab your buttons at the top of the doc.
  const answer1 = document.querySelector("#answer1");
  const answer2 = document.querySelector("#answer2"); ...
  1. Write a function to handle the event listeners for each answer buttons
  answer1.addEventListener("click", function() {
    if (answer1.textContent == originalFixedAnswers[0]) {
      answer1.style.backgroundColor = "green";
    } else {
      answer1.style.backgroundColor = "red";
    }
  });
  1. After shuffling your answers in the updateDom function use a loop (e.g. forEach) to cycle through the answers and change textContent / attach event listeners

But as I said, it's already really nice with lots of different functions calling each other as and when rather than a single massive block of code!

@freemvmt freemvmt added enhancement New feature or request compliment labels Nov 15, 2019
@freemvmt
Copy link
Author

Apologies that this is exactly the same as @AlexandraOM's issue #48

@samjam48
Copy link

Agreed.

Your code is very readable and quite nice despite not being perfect. There is nothing annoying in it.

But yes as indicated this could be in a for each loop. if you're doing something explicitly 3 times or more you should usually automate it

 answer1.addEventListener("click", function() {
    answer2.style.pointerEvents = "none";
    answer3.style.pointerEvents = "none";
    answer4.style.pointerEvents = "none";

    if (answer1.textContent == originalFixedAnswers[0]) {
      answer1.style.backgroundColor = "#58AD58";
      question.textContent = "";
      let gif = document.createElement("img");
      gif.src = giffySrc;
      question.appendChild(gif);
      question.classList.add("gif");
    } else {
      answer1.style.backgroundColor = "#B93D41";
      question.classList.remove("gif");
      question.textContent = currentQuote;
      question.classList.add("insult");

    }
  });

  answer2.addEventListener("click", function() {
    answer1.style.pointerEvents = "none";
    answer3.style.pointerEvents = "none";
    answer4.style.pointerEvents = "none";

    if (answer2.textContent == originalFixedAnswers[0]) {
      answer2.style.backgroundColor = "#58AD58";
      question.textContent = "";
      let gif = document.createElement("img");
      gif.src = giffySrc;
      question.appendChild(gif);
      question.classList.add("gif");
    } else {
      answer2.style.backgroundColor = "#B93D41";
      question.classList.remove("gif");
      question.textContent = currentQuote;
      question.classList.add("insult");
    }
  });

  answer3.addEventListener("click", function() {
    answer1.style.pointerEvents = "none";
    answer2.style.pointerEvents = "none";
    answer4.style.pointerEvents = "none";

    if (answer3.textContent == originalFixedAnswers[0]) {
      answer3.style.backgroundColor = "#58AD58";
      question.textContent = "";
      let gif = document.createElement("img");
      gif.src = giffySrc;
      question.appendChild(gif);
      question.classList.add("gif");
    } else {
      answer3.style.backgroundColor = "#B93D41";
      question.textContent = currentQuote;
      question.classList.remove("gif");
      question.classList.add("insult");
      question.classList.add("gif");
    }
  });

  answer4.addEventListener("click", function() {
    answer1.style.pointerEvents = "none";
    answer2.style.pointerEvents = "none";
    answer3.style.pointerEvents = "none";

    if (answer4.textContent == originalFixedAnswers[0]) {
      answer4.style.backgroundColor = "#58AD58";
      question.textContent = "";
      let gif = document.createElement("img");
      gif.src = giffySrc;
      question.appendChild(gif);
      question.classList.add("gif");
    } else {
      answer4.style.backgroundColor = "#B93D41";
      question.classList.remove("gif");
      question.textContent = currentQuote;
      question.classList.add("insult");
    }
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compliment enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants