Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!