Skip to content

Instantly share code, notes, and snippets.

@oaugusto256
Created February 11, 2021 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oaugusto256/19cbedca4f6ff3db2244e7ae699546c4 to your computer and use it in GitHub Desktop.
Save oaugusto256/19cbedca4f6ff3db2244e7ae699546c4 to your computer and use it in GitHub Desktop.
Nubank Hackerrank challenge solution ~ ASCII to Text
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