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

168
Visualizações
Javascript: When all buttons are clicked function

Hello sir I'm practicing my vanilla javascript. I am trying to create a "Hangman Game" when the correct letters are clicked from the buttons.. You win. I have all these buttons in queryselector(letterA to letterZ).

The problem using this function.. the winAlert = visible implements when the letterS was clicked (last letter of "cautious").

So is there a way when you clicked all the letters of cautious in any order the winAlert.style.visibility = "visible" will be execute inside the function?

I've tried everything in the last hours.. buttons.forEach, slice etc. nothing works.

function youWonCautious() {
  var buttons = [letterC, letterA, letterT, letterI, letterU, letterI, letterO, letterS]
  for (var i = 0; i < buttons.length; i++) {
    var button = buttons[0, 1, 2, 3, 4, 5, 6, 7];

    button.onclick = function() {
      winAlert.style.visibility = "visible"

    }
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Buttons don't automatically maintain any state about whether they were clicked. You can do this yourself using the dataset property of the button.

Then your function can test whether all the buttons have this property set.

Or maybe you can change your buttons to checkboxes, and use the checked property.

function allButtonsClicked(buttons) {
  return buttons.every(b => b.dataset.chosen == 'true')
}

function youWonCautious() {
  var buttons = [letterC, letterA, letterT, letterI, letterU, letterO, letterS];
  buttons.forEach(button => {
    button.dataset.chosen = 'true';
    if (allButtonsClicked(buttons)) {
      winAlert.style.visibility = "visible";
    }
  });
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a global array in which log the alphabets clicked by the user. check for the alphabets in the global array variable and answer array. If matches Pass alert message else fail message to user.

about 4 years ago · Juan Pablo Isaza Relatório
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