Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created January 25, 2023 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDobby/d2a2baa93aa2b7af73b2c9d7bcf230bf to your computer and use it in GitHub Desktop.
Save ChrisDobby/d2a2baa93aa2b7af73b2c9d7bcf230bf to your computer and use it in GitHub Desktop.
You are given a list of positive integers which represents some range of integers which has been truncated. Find the missing bits, insert ellipses to show that that part has been truncated, and print it. If the consecutive values differ by exactly two, then insert the missing value.
const fillGap = (num: number, prev: number) => (num - prev === 2 ? `${num - 1},${num}` : `...,${num}`)
const missingBits = (numbers: number[]) =>
`[${numbers
.map((num, index, arr) => ({ num, prev: arr[index - 1] }))
.map(({ num, prev }) => (num - prev === 1 ? num : fillGap(num, prev)))
.join(',')}]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment