DEV Community

Discussion on: JavaScript Programming Problem

Collapse
 
lionelrowe profile image
lionel-rowe

Great idea for a series! But I think the question is a little under-specified. It'd be helpful to state that the output should also be a string (not an array), that alphabet places are 1-indexed, that each one must be followed by a space, and that you only need to handle lowercase Latin-alphabet letters without diacritics.

Anyway, here's my attempt:

const a = 'a'.codePointAt(0)
const alphaPos = str => str.replace(
    /[a-z]/g,
    ch => `${ch.codePointAt(0) - a + 1} `
)
Enter fullscreen mode Exit fullscreen mode