‹ back to the board

Monday, September 28

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.

  • slicing and indexing
  • Novice

examples

  1. case 1staves of ["oak", "ash", "elm", "yew", "fir"]["elm"] — five staves, so one gets the iron
  2. case 2staves of ["oak", "ash", "elm", "yew"]["ash", "elm"] — four staves, so the two in the middle, in order
  3. case 3staves of ["lone"]["lone"] — a single stave is its own middle
  4. case 4staves of [][] — no staves, no brand
reward
130 XP
signature
middleStaves
not yet run

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
The Cooper's Middle Stave · bitwick