-2

I am trying to write a basic JavaScript code which would act as a 'translator'. For instance certain strings in the input text-box would correspond to specific strings in the output paragraph. So, for example if the word 'x' corresponds to the word 'y', then I want a code so that whenever it finds the word 'x' in the input it will replace it with the word 'y' in the output paragraph.

Here for example is how it might look like.

What would be the simplest way to implement this? And, if there happens to be a large number of translation pairs, what would be the most efficient way of listing them?

Any guidance would be greatly appreciated.

EDIT:

This is how I defined the elements within the code so far:

<script type="text/javascript">
function write_word(){
    var trans_parra = document.getElementById('trans');
    var word = document.getElementById('word');
    var eng = document.getElementById('input');}
</script>

And in the html body:

<p align="center"><input type="button" value="Search" onclick="write_word();"/>

Merin
  • 55
  • 8
  • Stack Overflow is not a code writing service. You need to show what you have already tried so far. Please read [How to Ask a Good Question](/help/how-to-ask) and provide an [mcve] – Tibrogargan Jul 16 '16 at 02:30
  • I was asking for some suggestions as where to start since I had no idea how to approach this. I was not asking for a coding service. So I simply described something like a pseudocode I had in mind. I will the code I already have to the post however, but I don't think it is very useful. – Merin Jul 16 '16 at 02:35
  • what would you suggest, what are your ideas.. how it could be done? – webdeb Jul 16 '16 at 02:37
  • An array of translation pairs, a loop, and a regex `.replace()`. – nnnnnn Jul 16 '16 at 02:38
  • @nnnnnn ... and fun – webdeb Jul 16 '16 at 02:39

1 Answers1

1

I'm assuming this is not a language translation tool you are making and that this is only to translate a line of text to something meaningful.

You can try Javascript Replace:

var strInput= "abcd x abcd";
var translated = strInput.replace("x", "y"); 

If you have a long list of characters to translate, then you can have an array of all the characters/words you would like to replace by extending the String object as mentioned in the answer of this question: Replace multiple strings at once

Community
  • 1
  • 1
Jomar Sevillejo
  • 1,648
  • 2
  • 21
  • 33
  • Thank you, this seems like a very good place to start. I am a complete beginner so I was just asking to be pointed in the right direction. – Merin Jul 16 '16 at 02:55
  • And looks like the link a gave you is a place to start :) Among all the Stack Exchange Forums I am in, StackOverflow has the most strict users trying to kick people out using the rules, so be careful. Asking too often and getting too many negative votes on those questions could get you an asking ban. You can read through the rules on what it is and how to remove it if you ever get it. Don't get me wrong though, StackOverflow is one of the best sites to grow programming skills, so Good Luck! – Jomar Sevillejo Jul 16 '16 at 07:11