Skip to content

Instantly share code, notes, and snippets.

@m0rsecode
Last active January 7, 2021 00:59
Show Gist options
  • Save m0rsecode/84c2c6c8fa815710d850b67ca29a9969 to your computer and use it in GitHub Desktop.
Save m0rsecode/84c2c6c8fa815710d850b67ca29a9969 to your computer and use it in GitHub Desktop.
function decipherThis(str) {
const alphabet = ['a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
const index = alphabet.reduce((acc, letter) => {
const uppercase = letter.toUpperCase()
acc[letter.charCodeAt(0)] = letter
acc[uppercase.charCodeAt(0)] = uppercase
return acc
}, {})
const words = str.split(' ')
const sentence = words.map((word) => {
const charCode = word.match(/\d+/)
const wordMatch = word.match(/[a-z]+/)
const reversedWord = wordMatch ? wordMatch[0] : ""
const partialWord = reversedWord.split("").reverse().join('')
return index[charCode] + partialWord
})
return sentence.join(' ')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment