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

243
Visualizações
How do I make this button respond and run the listed function?

I would like the button I created to simply document "Heads" or "Tails" based off my function.

<button id="coinFlipper">Flip a Coin</button>

<script>
const coinFlip = function() {
  let randomNumber = Math.random()
  if (randomNumber < 0.5) {
    document.write("Heads")
  } else {
    document.write("Tails")
  }
}
</scipt>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So First you need to create another element where you would display your result, than you need to create a function and assing this function on to a button with the onclick event like so -> if you want to change the text in the button just change the id of the button

    const coin =()=>{
    let randomNumber = Math.random();
    if (randomNumber < 0.5) {
        document.getElementById("result").innerHTML = "Heads";
    } else {
        document.getElementById("result").innerHTML = "Tails";
  }
}
    <button onclick="coin()" >Flip a Coin</button>
    <p id="result"></p>

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