Skip to content

Commit 9126c4b

Browse files
prep wolf sheep array exercise try 1
1 parent 595fb01 commit 9126c4b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

restOfThe8kyus/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</head>
1010

1111
<body>
12-
<script src="js/wideMouthedFrog.js"></script>
12+
<script src="js/wolfInSheepClothing.js"></script>
1313
</body>
1414

1515
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// parameters - array
2+
//return - string
3+
4+
function warnTheSheep(queue) {
5+
//todo identify location of the wolf
6+
//todo check against length of array
7+
//todo if length is greater than location of the wolf --> warn sheep of index wolf + 1
8+
//todo if wolf index is last element of array --> pls go away message
9+
//todo
10+
11+
let wolfPosition = queue.indexOf("wolf");
12+
if (queue.length - 1 == wolfPosition) {
13+
return `Pls go away and stop eating my sheep`;
14+
}
15+
if (queue.length > wolfPosition) {
16+
return `Oi! Sheep number ${
17+
queue.length - 1 - wolfPosition
18+
}! You are about to be eaten by a wolf!`;
19+
}
20+
}
21+
22+
console.log(
23+
warnTheSheep([
24+
"sheep",
25+
"sheep",
26+
"sheep",
27+
"sheep",
28+
"sheep",
29+
"wolf",
30+
"sheep",
31+
"sheep",
32+
])
33+
);
34+
// ["sheep", "sheep", "sheep", "sheep", "sheep", "wolf", "sheep", "sheep"],
35+
// "Oi! Sheep number 2! You are about to be eaten by a wolf!"
36+
// );
37+
38+
console.log(
39+
warnTheSheep(["sheep", "wolf", "sheep", "sheep", "sheep", "sheep", "sheep"])
40+
);
41+
// ["sheep", "wolf", "sheep", "sheep", "sheep", "sheep", "sheep"],
42+
// "Oi! Sheep number 5! You are about to be eaten by a wolf!";
43+
console.log(
44+
warnTheSheep(["wolf", "sheep", "sheep", "sheep", "sheep", "sheep", "sheep"])
45+
);
46+
// ["wolf", "sheep", "sheep", "sheep", "sheep", "sheep", "sheep"],
47+
// "Oi! Sheep number 6! You are about to be eaten by a wolf!";
48+
console.log(warnTheSheep(["sheep", "wolf", "sheep"]));
49+
// ["sheep", "wolf", "sheep"],
50+
// "Oi! Sheep number 1! You are about to be eaten by a wolf!";
51+
console.log(warnTheSheep(["wolf"]));
52+
// ["wolf"], "Pls go away and stop eating my sheep";
53+
console.log(warnTheSheep(["sheep", "sheep", "wolf"]));
54+
// ["sheep", "sheep", "wolf"], "Pls go away and stop eating my sheep";

0 commit comments

Comments
 (0)