Forum Discussion
_anomDiebolt_
7 years agoQrew Elite
There are several problems with your proposed encoding:
(1) what happens to upper case letters or characters outside the range a-z?
(2) your encoding "10312222850417118122" for "a catthe doga rat" is wrong as "t" is the 20'th letter of the alphabet not the 22'nd. There are more errors later in the string.
(3) if you intend to decode your string at some point it isn't clear how say the sub-string "22" would be decoded as it could be "bb" or "v".
In any event this fragment of code patches some of these issues and deals with issue (3) by inserting a space delimiter between the encodings for individual characters:
(1) what happens to upper case letters or characters outside the range a-z?
(2) your encoding "10312222850417118122" for "a catthe doga rat" is wrong as "t" is the 20'th letter of the alphabet not the 22'nd. There are more errors later in the string.
(3) if you intend to decode your string at some point it isn't clear how say the sub-string "22" would be decoded as it could be "bb" or "v".
In any event this fragment of code patches some of these issues and deals with issue (3) by inserting a space delimiter between the encodings for individual characters:
var string = "a catthe doga rat".toLowerCase();
string = string.replace(/./g, function(c) {
var charCode = c.charCodeAt(0);
if (charCode == 32) {
return 0 + " ";
} else {
return c.charCodeAt(0) - 96 + " ";
}
});
console.log(string);
//output:
//1 0 3 1 20 20 8 5 0 4 15 7 1 0 18 1 20