Search code examples
javascripthtmlencoderdecoder

how can i make a decorder and encoder?


I made my own digital language and i want to make a decoder and an encoder for that, so for example when i write down

"bees"

it gives me something like

"$@@%"

and when i write `

"$@@%"

` it gives me

"bees"

so it encodes things according to the info i give my website like i tell my website that $ is B and other letters are other symbols. how can i make this in html and javescript?


Solution

  • i suppose you just need a 1 to 1 mapping, and then a method to encode and one to decode your text.

    probably something similar to this

    const alphabet = { 'a': '$', 'b': '?'} //here you define your mapping
    
    encode = text => text.split('').map(ch => alphabet[ch]).join('');
    
    decode = text => text.split('').map(ch => Object.keys(alphabet)
    .find(letter => alphabet[letter] === ch)).join('');
    
    console.log(encode('ab'));
    console.log(decode('$?'));