DEV Community

Discussion on: Daily Challenge #60 - Find the Missing Letter

Collapse
 
willsmart profile image
willsmart

A quicky using reduce in JS

First up it converts the array of letters to an array of char codes.
It then uses reduce to find the 2-code gap, and return the letter with char code one less that that of the letter just after the gap.

If there is more than one gap it will return the letter for the last one.
If there is no 2-code gap, then it will return undefined.

findMissingLetter = letters =>
  letters
    .map(c => c.charCodeAt(0))
    .reduce(
      (acc, code, index, codes) => (index && code == codes[index - 1] + 2 ? String.fromCharCode(code - 1) : acc),
      undefined
    );

(checked on kata)