Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!