Novicesyntax & basics
The Cooper's Middle Stave
The cooper of bitwick brands the middle of every barrel she builds, and she needs a tally of which staves get the iron.
A barrel is given as a list of strings, each the name of one stave, laid out in the order they sit around the hoop.
Write middleStaves(staves), which takes that list and returns a new list of strings:
- If the list has an odd number of staves, return a list containing only the single middle stave.
- If the list has an even number of staves, return a list containing the two middle staves, in the same order they appear in the input.
- If the list is empty, return an empty list.
The input list must not be modified.
examples
- case 1
staves of ["oak", "ash", "elm", "yew", "fir"]["elm"] — five staves, so one gets the iron - case 2
staves of ["oak", "ash", "elm", "yew"]["ash", "elm"] — four staves, so the two in the middle, in order - case 3
staves of ["lone"]["lone"] — a single stave is its own middle - case 4
staves of [][] — no staves, no brand
- reward
- 130 XP
- signature
middleStaves
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- odd count of five
- even count of four
- single stave
- empty barrel
- exactly two staves
- even count of six
- odd count of seven