JSFiddle - React, Tailwind, and code Playground

by lavisha99

JavaScript

function flipcoin(nothrow) {
let coinarray=[];
for(var i=0; i<nothrow; i++){
let randno= Math.floor(Math.random()*2+1);
if(randno===1){
cointoss='H';
}else
{cointoss='T';}
coinarray.push(cointoss);
}
return coinarray;
} 
console.log(flipcoin(6));
/*
Write a function to flip a coin and keep going until you have 6 heads. Return the list as an array. Call the function a number of times to compare the result
*/
function flipcon(){
let coinArray= [];
let noHeads=0;
do {
 rando=Math.floor(Math.random()*2+1);
cointosss= (rando===1) ? 'H': 'T' ;
noHeads+= (cointosss==='H') ? 1:0;
coinArray.push(cointosss)
} while (
noHeads< 6
);
return coinArray;
}
console.log(flipcon());