Javascript - Rigged coin flip (heads first)

Sergi

Really basic function, I have a button that simulates a coin flip, I want to always show "heads" first and then randomize after that first input, current code does not work and it randomizes from the first click.

HTML:

<button id = "riggedCoin" onclick = "riggedCoinFlip()"> Rigged Coin </button>

<h1 id ="h2"> Coin Flip </h1>

JS:

function riggedCoinFlip() {

document.getElementById("h2").innerHTML = ("Heads!");

var coinFace = Math.floor(Math.random() * 2);

if (coinFace === 0) {
var h1 = document.getElementById("h2").innerHTML = ("Heads!");
} else {
var h1 = document.getElementById("h2").innerHTML = ("Tails!");

 }
}
inanc

At first, #h2 element already contains Coin Flip text. So, we can use this to check for the first time. We don't need another variable to check it.

I've simplified the solution for you. You can try:

function riggedCoinFlip() {
  var el = document.getElementById("h2");

  if (el.innerHTML === " Coin Flip ") {
    el.innerHTML = "Heads!";
  } else {
    el.innerHTML = (Math.floor(Math.random() * 2) === 0
      ? "Heads" : "Tails") +"!";
  }
}
<button id="riggedCoin" onclick="riggedCoinFlip()">Rigged Coin</button>
<h1 id="h2"> Coin Flip </h1>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Coin flip simulation never exceeding a streak of 15 heads

From Dev

python - infinite coin flip that stops when number of heads = number of tails

From Dev

python - infinite coin flip that stops when number of heads = number of tails

From Dev

Coin Flip HTML / Javascript | (Help)

From Dev

Using a button to display the outcome of a coin flip Javascript

From Dev

Guess a number (rigged) - Javascript

From Dev

Tallying the outcome of a coin flip

From Dev

Coin flip command

From Dev

How flipping a coin and display tails and heads counting?

From Dev

Flip coin simulation with R programming

From Dev

Coin flip program on c++

From Dev

coin flip program problems, C programming

From Dev

Debugging a Biased Coin Flip (C++)

From Java

Flipping a coin and stopping when it lands heads 4 times in a row

From Dev

Homework: Simulating coin tosses until consecutive heads using R

From Dev

How to find the most frequent length of Heads in a Coin Toss?

From Dev

Coin toss with JavaScript and HTML

From Dev

Coin toss with JavaScript and HTML

From Dev

Coin flip, which way is would produce a more accurate 50%

From Dev

Coin change, dynamic programming, but coin value reduces after first use

From Dev

Coin change, dynamic programming, but coin value reduces after first use

From Dev

Javascript Image Replace Coin Flipping

From Dev

coin flip experiment in C++ using std::rand() giving incorrect result

From Dev

I want to run this flip coin animation continuously from on-create method.How to do it

From Dev

Javascript Coin changing / Change making algorithm

From Dev

JavaScript - What's wrong with this coin change algorithm

From Dev

Implementing a recursive coin change function in Javascript

From Dev

Triggering a flip animation in CSS3 with Javascript

From Dev

Canvas/JavaScript - flip color of a square on mouse click

Related Related

  1. 1

    Coin flip simulation never exceeding a streak of 15 heads

  2. 2

    python - infinite coin flip that stops when number of heads = number of tails

  3. 3

    python - infinite coin flip that stops when number of heads = number of tails

  4. 4

    Coin Flip HTML / Javascript | (Help)

  5. 5

    Using a button to display the outcome of a coin flip Javascript

  6. 6

    Guess a number (rigged) - Javascript

  7. 7

    Tallying the outcome of a coin flip

  8. 8

    Coin flip command

  9. 9

    How flipping a coin and display tails and heads counting?

  10. 10

    Flip coin simulation with R programming

  11. 11

    Coin flip program on c++

  12. 12

    coin flip program problems, C programming

  13. 13

    Debugging a Biased Coin Flip (C++)

  14. 14

    Flipping a coin and stopping when it lands heads 4 times in a row

  15. 15

    Homework: Simulating coin tosses until consecutive heads using R

  16. 16

    How to find the most frequent length of Heads in a Coin Toss?

  17. 17

    Coin toss with JavaScript and HTML

  18. 18

    Coin toss with JavaScript and HTML

  19. 19

    Coin flip, which way is would produce a more accurate 50%

  20. 20

    Coin change, dynamic programming, but coin value reduces after first use

  21. 21

    Coin change, dynamic programming, but coin value reduces after first use

  22. 22

    Javascript Image Replace Coin Flipping

  23. 23

    coin flip experiment in C++ using std::rand() giving incorrect result

  24. 24

    I want to run this flip coin animation continuously from on-create method.How to do it

  25. 25

    Javascript Coin changing / Change making algorithm

  26. 26

    JavaScript - What's wrong with this coin change algorithm

  27. 27

    Implementing a recursive coin change function in Javascript

  28. 28

    Triggering a flip animation in CSS3 with Javascript

  29. 29

    Canvas/JavaScript - flip color of a square on mouse click

HotTag

Archive