I have a code that replaces the input string and replaces the specific words with other words. This works when I specific single words in the input
Example:
"BROWN" is replaced by "soil colored"
But if I add:
"a quick brown fox jumps over a lazy dog" and add "a quick bwown fox jumps ovew a wazy good boy", it does not work and shows the input in output field instead of the specified output.
Here is my code:
HTML:
<input id="input_1" type="text" value="" oninput="funcinput1()">
<button id="button_1 ml-auto copy-btn btn_cpy">translate</button>
<textarea id="output_1"></textarea>
JS:
function funcinput1()
{
let translator = {
"a quick brown fox jumps over a lazy dog": "a quick bwown fox jumps ovew a wazy good boy",
"BROWN": "soil colored",
"quick": "fast",
"the": "dee",
"n":"\ud83d\udc68\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c\udffd"
};
var input = document.querySelector("#input_1");
var output = document.querySelector("#output_1");
input.addEventListener("input", () => {
let words = input.value.split(' ');
let translated = words.map(word => translator[word] || word);
output.value = translated.join(' ');
})
}
I am still a beginner so any help to resolve this issue would be appreciated.
Here's a Stack Snippet:
function funcinput1()
{
let translator = {
"a quick brown fox jumps over a lazy dog": "a quick bwown fox jumps ovew a wazy good boy",
"BROWN": "soil colored",
"quick": "fast",
"the": "dee",
"n":"\ud83d\udc68\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c\udffd"
};
var input = document.querySelector("#input_1");
var output = document.querySelector("#output_1");
input.addEventListener("input", () => {
let words = input.value.split(' ');
let translated = words.map(word => translator[word] || word);
output.value = translated.join(' ');
})
}
<input id="input_1" type="text" value="" oninput="funcinput1()">
<button id="button_1 ml-auto copy-btn btn_cpy">translate</button>
<textarea id="output_1"></textarea>
split(' ')call - pilchard