|
| 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