How to Create a 50/50 Chance in JavaScript?

Estimated read time 1 min read

To create a 50/50 chance in JavaScript, you can use the Math.random function, which returns a random number between 0 and 1.

Here’s an example of how you can create a 50/50 chance in JavaScript:

let chance = Math.random();

if (chance < 0.5) {
  console.log("You got heads!");
} else {
  console.log("You got tails!");
}

This code will randomly generate a number between 0 and 1, and if the number is less than 0.5, it will output “You got heads!”, and if the number is greater than or equal to 0.5, it will output “You got tails!”. This simulates a 50/50 chance.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply