Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Javascript for loop iteration. Something is wrong

I have list of objects named data.

And 1 input tag.

I want to check that if input value exists in data's person1 key. If exist I write input value and person2 keys value. Else write I don't understand.

How this code gives true answer. this is wrong.

Try How old are you this. If you input this it must give I don't understand 2 times and 30

function SendMessage() {
    var input = document.getElementById('person'),
        sender = document.createElement('p'),
        receiver = sender.cloneNode(true),
        data = [{
            "person1": "Hey",
            "person2": "Hey, How are you?"
        }, {
            "person1": "Hello",
            "person2": "Hello friend"
        }, {
            "person1": "How old are you",
            "person2": "30"
        },
    ];

    sender.innerText = input.value;
    document.body.appendChild(sender);
    for (var i = 0; i < data.length; i++) {
        if (input.value === data[i].person1) {
            receiver.innerText = data[i].person2;
            document.body.appendChild(receiver);
            i = data.length;
        } else {
            receiver.innerText = "I don't understand";
            document.body.appendChild(receiver);
        }
    }
}
<input id="person">
<button onclick="SendMessage()">Send message</button>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found my missed code. for loop works fine. I create element before for loop. And every iteration I just changes that elements innerText. It writes I don't understand. But when it finds correct answer. It overwrites the answer.

 

function SendMessage() { 
    var input = document.getElementById('person'), 
    sender = document.createElement('p'), 
    receiver = sender.cloneNode(true), 
    data = [{ "person1": "Hey", "person2": "Hey, How are you?" }, 
            { "person1": "Hello", "person2": "Hello friend" }, 
            { "person1": "How old are you", "person2": "30" }, ]; 
    sender.innerText = input.value; 
    document.body.appendChild(sender); 
    for (var i = 0; i < data.length; i++) { 
        if (input.value === data[i].person1) { 
            receiver.innerText = data[i].person2; 
            console.log(data[i].person2); 
            document.body.appendChild(receiver); 
            i = data.length; 
        } else { 
            console.log("I don't understand"); 
            receiver.innerText = "I don't understand"; 
            document.body.appendChild(receiver); 
        } 
    } 
} 
 
<input id="person"> 
<button onclick="SendMessage()">Send message</button> 

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda