In this problem of decryption of a message, I have to assign each Encryption Key character to an English alphabet without hard coding with the following criteria: A key would have at least one instance of each letter in the alphabet(without whitespaces!) I tried to do it by creating a map but with the repetition of a character(not to be included again) in key the actual alphabet also gets move ahead. How can it be achieved?
key = "the quick brown fox jumps over the lazy dog"
var decodeMessage = function (key, message) {
let sub = new Map();
key = key.replace(/\s/g, "");
for (let i = 0; i < key.length; i++) {
if (sub.has(key[i])) {
continue;
} else {
sub.set(key[i], String.fromCharCode(97 + i));
}
}
return sub;
};
Output:
Map {
't' => 'a',
'h' => 'b',
'e' => 'c',
'q' => 'd',
'u' => 'e',
'i' => 'f',
'c' => 'g',
'k' => 'h',
'b' => 'i',
'r' => 'j',
'o' => 'k',
'w' => 'l',
'n' => 'm',
'f' => 'n',
'x' => 'p',
'j' => 'q',
'm' => 's',
'p' => 't',
's' => 'u',
'v' => 'w',
'l' => '}',
'a' => '~',
'z' => '',
'y' => '€',
'd' => '',
'g' => 'ƒ' }