Created
February 11, 2021 14:41
-
-
Save oaugusto256/19cbedca4f6ff3db2244e7ae699546c4 to your computer and use it in GitHub Desktop.
Nubank Hackerrank challenge solution ~ ASCII to Text
This file contains 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
const getCurrentCharValue = (str, startIndex, endIndex) => { | |
const currentValueToDecode = str.substring(startIndex, endIndex); | |
const currentValueToDecodeInt = parseInt(currentValueToDecode); | |
return String.fromCharCode(currentValueToDecodeInt); | |
} | |
const getCurrentIndexJump = (value) => { | |
if (value === 32) { | |
return 1; | |
} | |
if (value >= 65 && value <= 90) { | |
return 2; | |
} | |
if (value >= 97 && value <= 122) { | |
return 3; | |
} | |
return 0; | |
} | |
function decode(encodedString) { | |
// Write your code here | |
const revertedEncodedString = encodedString.split("").reverse().join(""); | |
let revertedEncodedStringLength = revertedEncodedString.length; | |
let decodedString = ''; | |
let startIndexToDecode = 0; | |
let endIndexToDecode = 0; | |
let currentValueToDecode = revertedEncodedString.substring(startIndexToDecode, endIndexToDecode); | |
let currentValueToDecodeInt = parseInt(currentValueToDecode); | |
while (endIndexToDecode !== revertedEncodedStringLength + 1) { | |
currentValueToDecode = revertedEncodedString.substring(startIndexToDecode, endIndexToDecode); | |
currentValueToDecodeInt = parseInt(currentValueToDecode); | |
if (getCurrentIndexJump(currentValueToDecodeInt) === 0) { | |
endIndexToDecode += 1; | |
} else { | |
if (getCurrentIndexJump(currentValueToDecodeInt) === 1) { | |
decodedString += getCurrentCharValue(revertedEncodedString, startIndexToDecode, endIndexToDecode); | |
startIndexToDecode = endIndexToDecode; | |
} else if (getCurrentIndexJump(currentValueToDecodeInt) === 2) { | |
decodedString += getCurrentCharValue(revertedEncodedString, startIndexToDecode, endIndexToDecode); | |
startIndexToDecode = endIndexToDecode; | |
} else if (getCurrentIndexJump(currentValueToDecodeInt) === 3) { | |
decodedString += getCurrentCharValue(revertedEncodedString, startIndexToDecode, endIndexToDecode); | |
startIndexToDecode = endIndexToDecode; | |
} | |
} | |
} | |
return decodedString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment