Write an application that will simulate a coin toss Use the

Write an application that will simulate a coin toss. Use the Math.random() function for the coin toss:

result = Math.random();

For the toss you may use the following criteria: if the result is 0.5 or less, the toss represents a head, otherwise it represents a tail.

Have a user input to determine how many tosses of the coin is desired. Generate the tosses and keep track of the number of heads and number of tails (do not display each toss). Display the percentage of heads and tails after the tosses are complete

Save the application as FlipCoin.java

Solution

function flipCoin(){return Math.random()<.5;} function getNumberOfCoins(){ var r=prompt(\"How many flips?\"); if(isNaN(r)){ alert(\"Please type a number!\"); return getNumberOfCoins(); } if(!r)r=0; return Math.floor(Number(r)); } function coinFlip() { var f=getNumberOfCoins(); var heads=0; var tails=0; for(var i=0;i
Write an application that will simulate a coin toss. Use the Math.random() function for the coin toss: result = Math.random(); For the toss you may use the foll

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site