Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Mousedown event for non-button + Correct format for memory game

I have repeatedly attempted to use a mousedown listener for this memory game, but it won't work.

Also, I feel there is a way to do the game with my layout of the game itself.

async function eachRounds() {
  for (let i = 0; i < rounds; i++) {
    for (let j = 0; j <= i; j++) {
      let currentStatus = document.getElementById("status");
      currentStatus.innerHTML = "Round " + (counter + 1) + " out of " + rounds;
      let random = Math.floor(Math.random() * array.length);
      await flash(array[random]);
      await revert();
      let prompted = await colorClicked();
      if (prompted == array[random]) {
        currentStatus.innerHTML = "On to the next round!";
        if (j == rounds && sequence[rounds - 1] == colorClicked()) {
          currentStatus.innerHTML = "Congratulations! You win!";
          gameOn = false;
        }
      } else {
        currentStatus.innerHTML = "Sorry! You lose!";
        gameOn = false;
      }
    }
  }
}

That is the game layout itself. gameOn is a global boolean variable that sets to true when the game starts in another method.

array is the list of colors, and sequence is the list of the ordered colors the users must replicate by clicking.

The event listener for making the colored panels act as "buttons" is listed down below.

async function colorClicked() {
  red.addEventListener("mousedown", async function () {
    await flash(red);
    await revert();
    return red;
  });
  blue.addEventListener("mousedown", async function () {
    await flash(blue);
    await revert();
    return blue;
  });
  green.addEventListener("mousedown", async function () {
    await flash(green);
    await revert();
    return green;
  });
  yellow.addEventListener("mousedown", async function () {
    await flash(yellow);
    await revert();
    return yellow;
  });
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda