I\'ve implemented the function:
function coinFlip() {
return(Math.floor(Math.random()*2) === 0) ? \'Heads\' : \'Tails\';
}
And it\'s al
If one of three toss coin is head it doesn't mean that in 10 toss, there will be 3 heads.. Here is your code with 500 toss (just change the number)
function coinFlip() {
return(Math.random() < 0.3) ? 'Heads' : 'Tails'; //ofc 0.3 is 30% (3/10)
}
var howManyTimes=500;
var countHeads=0;
for (var i=0; i<howManyTimes;i++){
if (coinFlip()==='Heads'){
countHeads++;
}
}
alert("Heads appear "+(countHeads/howManyTimes)*100+"% of the time");
"how to solve a specific percentage problem"
You can't, this is how probability works
function coinFlip() {
return(Math.random() < 0.3) ? 'Heads' : 'Tails';
}