title: How do I use a boolean flag within a for loop cycle?
body: <p>I want to call a function (displaying a modal) when a specific condition is fulfilled (two strings matching with each other).
  The conditions change and are set within a for loop cycle (it loops the elements of two JSON files).</p>
  
  <p>The possible conditions are three:</p>
  
  <p>1) no strings typed, no match, no problem;</p>
  
  <p>2) I type a string: the for loop cycle tells me that the strings don't match;</p>
  
  <p>3) I type a string: the for loop cycle tells me that the strings match.</p>
  
  <p>The second and third conditions are the problematic ones for they are inside the for loop cycle and I need to call a specific action for each of them, by avoiding to repeat the action for every element in the cycle when condition 2) is fulfilled.</p>
  
  <p>I want to set a <strong>boolean flag</strong> but I'm not sure I understood how should I use it.
  I created a variable outside the for loop and gave it the value "false".
  Inside the for loop, I recall such a variable by giving it the value "true".
  Outside the second for loop, I call it inside an if statement which calls a function to show the modal.</p>
  
  <p>I wrote the following code:</p>
  
  <pre><code>function validateCitta() {
      $("#cercaCitta").prop("disabled", false);        
      let text = $('#inlineFormInputCitta').val();
      if (text === "") {
          $("#errorLog").show();
      }
      else {   
  
  
          var check = false;    //  I set the flag variable 
  
  
          $.ajax({
              url: "https://nominatim.openstreetmap.org/search?q=" + encodeURIComponent($("#inlineFormInputCitta").val()) + "&amp;format=geocodejson",
              dataType: "json",
              success: function (data) {
                  for (let i = 0; i &lt; data.features.length; i++) {
                      let typeCity = data.features[i].properties.geocoding.type;
                      if (typeCity === "city") {
                          let nameCity = data.features[i].properties.geocoding.name;
                          for (let i = 0; i &lt; json.tappe.length; i++) {
                              let tappa = json.tappe[i];
                              let city = json.tappe[i].city;
                              if (city === nameCity) {
                                  console.log("JSON file has been activated");
  
  
                                  check = true;  // the condition 3 is fulfilled, then I perform the action below:
  
                                  $("#tbody").append("&lt;tr&gt;&lt;td&gt;" + tappa.name + "&lt;/td&gt;&lt;td&gt;" + tappa.state + "&lt;/td&gt;&lt;td&gt;" + tappa.region + "&lt;/td&gt;&lt;td&gt;" + tappa.city + "&lt;/td&gt;&lt;/tr&gt;");                                    
                              }    
                              else if (check){     // the condition 3 is false, the condition 2 is fullfilled, then I show the Modal:
                                  $('#myModal').show();
                              }    
                              ;
                          }                           
                          ;
                      }
                  }
              }
          })           
      }
  };
  </code></pre>
  
  <p>Which is the correct way to set and call a boolean flag? 
  Can you show me some examples to better understand the usage of such a particular variable and the differences between using it from scratches and within a cycle?</p>