-
-
Save gpetrioli/cfc05bb447f2706f2de833c9cea4e351 to your computer and use it in GitHub Desktop.
Oldschool solution
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
| // In reply to https://gist.github.com/codepo8/9e7dcabb2a9b0c8f3c19441563c6318c | |
| const OldSchoolMobile = { | |
| keyboard: [' ', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'], | |
| decode(sequence) { | |
| const letters = sequence.split('-').map((presses) => { | |
| const key = Number(presses[0]); | |
| const characters = OldSchoolMobile.keyboard[key]; | |
| const repeats = (presses.length - 1) % characters.length; | |
| return characters[repeats]; | |
| }); | |
| return letters.join(''); | |
| }, | |
| encode(string) { | |
| const letters = string.split(''); | |
| const presses = letters.map(letter => { | |
| const key = OldSchoolMobile.keyboard.findIndex(characters => characters.includes(letter)); | |
| return key.toString().repeat(OldSchoolMobile.keyboard[key].indexOf(letter) + 1); | |
| }) | |
| return presses.join('-'); | |
| } | |
| }; | |
| console.log(OldSchoolMobile.decode('44-33-555-555-666')); // hello | |
| console.log(OldSchoolMobile.decode('9-666-777-555-3')); // world | |
| console.log(OldSchoolMobile.decode('9-33-0-2-777-33-0-3-33-888-33-555-666-7-33-777-7777')); // we are developers | |
| console.log(OldSchoolMobile.encode('hello')); // 44-33-555-555-666 | |
| console.log(OldSchoolMobile.encode('world')); // 9-666-777-555-3 | |
| console.log(OldSchoolMobile.encode('we are developers')); // 9-33-0-2-777-33-0-3-33-888-33-555-666-7-33-777-7777 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment