Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions projects/m1/031-coin-flip-simulation/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const NUM_EQUALS= 3;
const NUM_TENTATIVI= 10;
function getRandomIntInclusive(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive
}

function main() {
const randomArray= getInput();

const lung = randomArray.reduce((acc,next)=>{
return acc+= next.length
},0)

console.log(`On average, ${lung /randomArray.length} flips were needed.`)
}
main();



function getRandomVal(){
return getRandomIntInclusive(0,1) === 0 ? 'T':'H'
}


function getRandom(array=[],count = 0){

if(count === NUM_EQUALS){
return array;
}

const randomVal = getRandomVal(0,1);
if(randomVal !== array.at(-1)){
count=1;
}
else{
count++;
}
array.push(randomVal);

return getRandom(array,count);
}



function getInput(array=[],obj={}){

if(array.length === NUM_TENTATIVI){
return array;
}

const randomArray = getRandom();
array.push(randomArray);
console.log(`${randomArray.join(' ')} (${randomArray.length} flips)`)

return getInput(array,obj)
}