Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
How javascript for loop checks statements without iteration

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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda