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

161
Views
How to make a loop in a javascript function using EJS

I've written this script. It works fine, but when I'm adding the loop it doesn't work. Is there a mistake in the structure of the loop or another thing?

<p id="f"></p>
<script>
    var resultat = '{ "name":"multiplication matricielle", "age":30, "city":"New York"}'
    var obj = JSON.parse(resultat);
    document.getElementById("id1").value = obj.name;
    document.getElementById("id2").value = obj.age;
    document.getElementById("id3").value = obj.city;
</script>


<script>
    function compare() {
        a = "yes";
        var i;
        for (i = 0; i < '<%=inf.length%>'; i++) {

            if ('<%=inf[i].nom%>' == obj.name && '<%=inf[i].sortie%>' == obj.city) {
                return a;
            }
        }
    }
    document.getElementById('f').innerHTML = compare();
</script>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have to return outside of your loop and you can change a little:

function compare() {
  var a = "No"; // <---initialize with default value
  var i;
  for (i = 0; i < parseInt('<%=inf.length%>', 10); i++) { // parse as number
    if ('<%=inf[i].nom%>' == obj.name && '<%=inf[i].sortie%>' == obj.city) {
      a = "Yes"; // if check is true change the value
    }
  }
  return a; // <--- now return it here
}

For your comment:
You need to parse a string number to a valid number

parseInt('<%=inf.length%>', 10); // You have to parse it as number.
about 4 years ago · Santiago Trujillo Report
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!