-
Notifications
You must be signed in to change notification settings - Fork 0
/
A-wolf-in-sheep's-clothing.js
29 lines (21 loc) · 1.24 KB
/
A-wolf-in-sheep's-clothing.js
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
/* Wolves have been reintroduced to Great Britain. You are a sheep farmer, and are now plagued by wolves which pretend to be sheep. Fortunately, you are good at spotting them.
Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing at the front of the queue which is at the end of the array:
[sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE)
7 6 5 4 3 2 1
If the wolf is the closest animal to you, return "Pls go away and stop eating my sheep". Otherwise, return "Oi! Sheep number N! You are about to be eaten by a wolf!" where N is the sheep's position in the queue.
Note: there will always be exactly one wolf in the array.
Examples
Input: ["sheep", "sheep", "sheep", "wolf", "sheep"]
Output: "Oi! Sheep number 1! You are about to be eaten by a wolf!"
Input: ["sheep", "sheep", "wolf"]
Output: "Pls go away and stop eating my sheep"
*/
//solution
function warnTheSheep(queue) {
var sheep = queue.length - queue.indexOf('wolf') - 1
if (sheep ==0 ){
return "Pls go away and stop eating my sheep"
} else {
return "Oi! Sheep number "+ sheep +"! You are about to be eaten by a wolf!"
}
}