0

I have 2 strings with some chars. One of them is with "mashed" characters, and the other one is with ordered characters which have some sense. For example:

wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!
Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT

So as you may see - the first one have equivalent of symbols which are the same for the equal symbol in the second one. And the task is - to create somehow kind of alphabet from them, because I have third one string wich have to be "decoded". (wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!). And the tricky part here is - that if I'm pretty sure for the first two strings that they're absolutely equal like a number of chars, so about the new one - is completely different number of symbols, but they consisting in the "alphabet"

And here is my current code for creation of the "alphabet":

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";

var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

var myenc = {};

var mynewenc = {};

for (i = 0; i < enc.length; i+=1) {
var encoded = new Array(enc[i]);
var decoded = new Array(dec[i]);


myenc[enc[i]] = dec[i];
};

console.log(myenc);

And now - how I have to decode, the new one string, using this "alphabet"?

Fire Bull
  • 227
  • 3
  • 10

3 Answers3

0

Here's one way that you can approach it (as I understand the question):

// Create your dictionary
var dict = {};

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!".split('');
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT".split('');

// Populate your dictionary
for (var i = 0; i < enc.length; i++) {
  dict[enc[i]] = dec[i];
}

// You can use your dictionary like this
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!".split('');

// Returns your translated string
newenc.map(function(e) {
  return dict[e];
}).join('');

However for this method you'll have to deal with characters that are defined in newenc that are not defined in your enc (such as T). I tried to do the best I could given the situation and rules that you've described, hope this helps!

Josh Black
  • 999
  • 2
  • 8
  • 17
0

If I understand well, you can try using this code. It is finding the appropriate encoded letter in your enc variable and if the letter is found, it is replacing it with the corresponding letter from your dec variable.

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";

var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

for (i = 0; i < newenc.length; i++) {
    for (j = 0; j < enc.length; j++) {
        if (enc[j] == newenc[i])
            newenc[i] = dec[j];
    }
};

console.log(newenc);

At the end your variable newenc may contain your decoded string.

Shoko
  • 133
  • 1
  • 12
0
var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

function make_dictionary(enc, dec){
    o = new Object();
    if(enc.length == dec.length){
        for(i=0; i<enc.length; i++){
            o[enc[i]] = dec[i];
        }
    }
    else{
        console.log('error');
    }
    return o;
}

function translate(newenc, dictionary, fill){
    var newstring = '';
    for(i=0; i<newenc.length; i++){
        if(typeof dictionary[newenc[i]] !== 'undefined'){
            newstring += dictionary[newenc[i]];
        }
        else{
            newstring += fill;
        }

    }
    return newstring;
}

var d = make_dictionary(enc, dec);
console.log(d);
var string = translate(enc, d, '_');
console.log(string);
var string = translate(newenc, d, '_');
console.log(string);