-3
Ex:
const array=[...'indice n','indice n2',...]
After modifying:
array = [...'maçã','indice n2,...']
function checkForTranslation(){
var input = document.getElementById('inputTextField').value
var outputDiv = document.getElementById('translationOutputDiv')
input = input.toLowerCase();
const newInput = input.split(space)
var index = newInput.indexOf('hi')
if(newInput.includes('hi')){
outputDiv.innerHTML = `${input.replace(/hi/g, '')}`
}
I know what’s inside the if
doesn’t make much sense, but it’s because I haven’t found a solution yet, but basically I’m trying to make a Translate, but whenever the word is written to be translated into the sentence it when replaced by the translation goes to the end of the sentence, so I thought to transform the string into an array to change the word value inside the array and return to string.
what you tried and what’s failing? You have to post some code so we can help. Have a look https://answall.com/help/how-to-ask
– Alexis Garcia
I made a change to the question, please look if you can understand better
– André Caracioly
vc can use a foreach, find the value and replace it, or use map https://stackoverflow.com/questions/35206125/javascript-es6-es5-find-in-array-and-change/35206193
– Marcos Brinner
If you want to swap a word from a string, you may not need to transform into array: https://answall.com/q/16963/112052 | https://answall.com/q/506410/112052
– hkotsubo
You have to close that question until the author of it improves! The text is a question about replacing elements in an array, the example presented is about manipulating HTML code. This is unfair to authors of answers because it implies that for those who answer it is subject to be negative or to follow what is in the text of the question and contradict the code of the example or to be negative or to follow what is in the code of the example and contradict the text of question.
– Augusto Vasques
You interpreted the question totally wrong then friend, I just presented the script that happens to contain HTML elements, but the question was 100% focused on the array, no HTML
– André Caracioly
Your question is confused and poorly elaborated and the function
checkForTranslation()
does not display an evident array interaction.– Augusto Vasques
"const newInput = input.split(space) "it here
– André Caracioly
Then edit the question and clarify and join a [MCVE].
– Augusto Vasques
To focus "100% on the array", you should take the whole "extra" part (like the
getElementById
andinnerHTML
, that depend on an HTML to test) and leave only the array itself. In fact, the problem itself is "how to replace words in a sentence" - the array is just the way you tried, but it’s not the only one (focusing on the problem instead of focusing on the solution attempt might be better for opening up more solution possibilities: for example, if the phrase is "Hi, all right" and you want to change the "Hi", it won’t work because thesplit
will separate "Hi," instead of just "Hi" - I don’t know if this applies to your case)– hkotsubo
I’m not really used to programming, let alone asking questions at stackoverlfow, so I didn’t know I had to follow so many protocols, I came up with a question and I was greeted with stone here, for more than one time.
– André Caracioly
At first it’s hard to get the hang of it anyway, but in time you get it. Remember that the idea of the site is that the questions are useful not only for you, but for any future visitor with the same problem. Hence we are a little "boring" and "rigid" with the format, because the focus is on the specific problem (in your case, replace words in a sentence, regardless of whether or not an HTML is involved), without "distractions" and "wrinkles". That said, an alternative without array: https://ideone.com/M4WbmQ
– hkotsubo
thanks friend, I tried to implement what Voce sent me but as I will use other translations in a sentence for now this way:
var index = newInput.forEach(function cada(item, indice){
 if(item == 'da'){
 newInput[indice] = 'good' 
 }
 
 });
– André Caracioly