Skip to content

Instantly share code, notes, and snippets.

@MaxTCodes
Created December 4, 2021 22:58
Show Gist options
  • Save MaxTCodes/b7a2635e50bde1db964e11ee87174ed7 to your computer and use it in GitHub Desktop.
Save MaxTCodes/b7a2635e50bde1db964e11ee87174ed7 to your computer and use it in GitHub Desktop.
Rabbit Solution
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