Last active
January 7, 2021 00:59
-
-
Save m0rsecode/84c2c6c8fa815710d850b67ca29a9969 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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