Created
September 28, 2021 06:34
-
-
Save blacksheep557/8ff9cc742b112496846a43f093bbf006 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(string) { | |
| const words = string.split(' '); | |
| return words.map(word => decode(word.split(''))).join(' '); | |
| } | |
| function decode(wordArr) { | |
| let hex = '' | |
| let secChar | |
| for (let [idx, char] of wordArr.entries()) { | |
| if (Number(char) || char == 0) { | |
| hex += char | |
| } else { | |
| secChar = idx | |
| break; | |
| } | |
| } | |
| let res = String.fromCharCode(hex) | |
| res += wordArr.length > hex.length ? wordArr.pop() : '' | |
| res += wordArr.length ? wordArr.splice(secChar + 1).join('') : '' | |
| res += wordArr[secChar] ? wordArr[secChar] : '' | |
| return res | |
| } | |
| function backSpace(string) { | |
| const resStack = [] | |
| for (const char of string) { | |
| if (char === '#') resStack.length && resStack.pop() | |
| else resStack.push(char) | |
| } | |
| return resStack.join('') | |
| } | |
| console.log(backSpace("abc#d##c"))// ==> "ac" | |
| console.log(backSpace("abc##d######"))// ==> "ac" | |
| console.log(backSpace("#######"))// ==> "ac" | |
| console.log(backSpace(""))// ==> "ac" | |
| // "abc##d######" ==> "" | |
| // "#######" ==> "" | |
| // "" ==> "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment