-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbit.js
More file actions
47 lines (43 loc) · 813 Bytes
/
rabbit.js
File metadata and controls
47 lines (43 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let pos = Math.floor(Math.random() * 30);
let len = 30;
let prev = pos;
console.log(pos);
console.log(prev);
function step() {
if (pos == len) {
pos--;
} else if (pos == 0) {
pos++;
} else if (Math.random() < 0.5) {
pos++;
} else {
pos--;
}
console.log("Rabbit is at ", pos);
}
let found = true;
for (let i = 0; i < len; i++) {
let attempt = i;
console.log(i);
if (attempt == pos) {
found = false;
console.log("found rabbit at", pos);
break;
}
step();
}
if (found) {
for (let i = 1; i < len; i++) {
let attempt = i;
console.log(i);
if (attempt == pos) {
found = false;
console.log("found rabbit at", pos);
break;
}
step();
}
}
if (found) {
console.log("game over");
}