-1

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?

  • 3
    I'm voting to close this as "too broad" because there are a ton of ways to implement something like this. "How do I get started" is not a good question here, rather try something and ask about problems you encounter. – Stephen Ostermiller Oct 22 '18 at 17:07

2 Answers2

0

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('$?'));
Karim
  • 8,454
  • 3
  • 25
  • 33
-1

Sounds like an interview question so I wont write it but you can use a simple replace method that you run the string through. it could be the same for both encode and decode IF you never use real letters in your language.

NOTE: javascript replace will only replace the first char it finds that matches you will need to use the regex version or loop over each char one at a time.

racamp101
  • 506
  • 2
  • 12