Created
December 4, 2021 22:58
-
-
Save MaxTCodes/b7a2635e50bde1db964e11ee87174ed7 to your computer and use it in GitHub Desktop.
Rabbit Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rabbitPos = Math.floor(Math.random() * 101); | |
function rabbitMove(rabbitPos) { | |
let e = Math.floor(Math.random() * 2); | |
switch (e) { | |
case 0: | |
if (rabbitPos >= 100) { | |
return (rabbitPos -= 1); | |
} else if (rabbitPos <= 0) { | |
return (rabbitPos += 1); | |
} else { | |
return (rabbitPos -= 1); | |
} | |
case 1: | |
if (rabbitPos >= 100) { | |
return (rabbitPos -= 1); | |
} else if (rabbitPos <= 0) { | |
return (rabbitPos += 1); | |
} else { | |
return (rabbitPos += 1); | |
} | |
} | |
} | |
function checkHole(num) { | |
if (num === rabbitPos) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
for (let i = 0; i < 99; i++) { | |
rabbitPos = rabbitMove(rabbitPos); | |
if (checkHole(i) || checkHole(i - 1)) { | |
console.log("You got the rabbit! on the " + i + "th try"); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment