From f46e3538a84b4410ea94dc5b0024192f95c23e36 Mon Sep 17 00:00:00 2001 From: Benito Rubano Date: Tue, 31 Dec 2024 14:10:20 +0100 Subject: [PATCH] solution/M1-031 --- .../m1/031-coin-flip-simulation/js/index.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/projects/m1/031-coin-flip-simulation/js/index.js b/projects/m1/031-coin-flip-simulation/js/index.js index e69de29bb2..6ec59d28b8 100644 --- a/projects/m1/031-coin-flip-simulation/js/index.js +++ b/projects/m1/031-coin-flip-simulation/js/index.js @@ -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) + } + +