title: How to set a variable to fulfill a condition outside a for loop cycle?
body: <p>problem:</p>
  
  <p>I created a search form where the user is asked to insert a string into an input form. The string is the name of the city and if it matches one of the 50 cities I have included into a JSON file some function is called.
  We may have three conditions:</p>
  
  <p>1)  The input form is left empty ------>  an error log appears.</p>
  
  <p>2)  The input form is not empty and the string matches one of the 50 strings in the JSON file ------>  a table is displayed.</p>
  
  <p>3)  The input form is not empty but the string doesn’t match any of the 50 strings in the JSON file ------>  a modal window is displayed</p>
  
  <p>My problem is how and where to write the command:</p>
  
  <pre><code>$(‘#Modal#).show()
  </code></pre>
  
  <p>In other terms, how and where should I show the modal window whenever the city inserted doesn’t match with any of those included in my JSON file?</p>
  
  <p>I have a <code>cycle for</code>: here the values of the strings in the JSON file are being checked; I wouldn’t put the command into there, otherwise the modal  will be called 49 times: since I have 50 cities, one of them matches the string inserted in the input form but the other 49 don't.</p>
  
  <p>I suppose I should create a new variable with a function outside the <code>for loop</code> cycle, setting the condition : "the string matches one and only one of the strings in the JSON file"; then the condition may be true inside the for loop (and then I display the table), whereas it's false outside the for loop (i.e. "if the number of cities found is 0" and then I show the modal window).</p>
  
  <p>The code I wrote so far is the following:</p>
  
  <pre><code>function validateCitta() {
    let text = $('#inlineFormInputCitta').val();
    if (text === "") {
      $("#errorLog").show();
    } else {
      $("#errorLog").hide();
      $.ajax({
        url: "https://nominatim.openstreetmap.org/search?q=" + encodeURIComponent($("#inlineFormInputCity").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");
                  $("#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;");
                  $("#tabella").show();
                };
              };
            }
          }
        }
      })
    }
  };
  </code></pre>
  
  <p>How may I set the new variable to fulfill the third 3) condition above?
  OR ALTERNATIVELY, would you have any other suggestion to show the modal window if the condition (3) is fulfilled?</p>
  
  <ul>
  <li><ul>
  <li><ul>
  <li>-E D I T E D - - - - </li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I edited the snippet as in the following:</p>
  
  <pre><code>function validateCitta() {    
  
      let text = $('#inlineFormInputCitta').val(); 
      var check = false;    
      if (text === "") {    
          $("#errorLog").show();    
      } else {    
          $("#errorLog").hide(); 
          $.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) {    
                                  var check = true;  
                                  $("#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;");    
                                  $("#tabella").show(); 
                              }
                              ;
                          }
                          ;   
                      }
                  }
              }
          })
          if (check) {
              $('#myModal').show();
          }
      }
  };
  </code></pre>
  
  <p>But it doesn't work.
  On the other hand, If I write </p>
  
  <pre><code>if (!check) {
                  $('#myModal').show();  
  </code></pre>
  
  <p>the modal is displayed also when the condition 2) is fulfilled... </p>
  
  <ul>
  <li><ul>
  <li><ul>
  <li>-E D I T E D   2 - - - - </li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I wrote the following code. <strong>It works, but</strong> I don't understand completely the role of the boolean flag <code>check</code> and the way its value changes inside and outside the for loop:</p>
  
  <pre><code>function validateCitta() {        
      let text = $('#inlineFormInputCitta').val();
  
      if (text === "") {            
          $("#errorLog").show();
      }   //condition 1: no strings, no problem
  
      else {                      
          $.ajax({
              url: "https://nominatim.openstreetmap.org/search?q=" + encodeURIComponent($("#inlineFormInputCitta").val()) + "&amp;format=geocodejson",
              dataType: "json",
              success: function (data) {
  
                  var check = false;     //I set the flag variable outside the cycle
  
                  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) {
  
                                  check = true;    
                 //conditon 3 is fullfilled: strings matches
  
  
                                  $("#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;");
                                  $("#tabella").show();                                    
                              }
                                  ;
                          }
                              ;
                      }
                  }    
                  if (!check) {   //does !check means that the value of 'check' is opposite to the one set at the beginning?
                      $('#myModal').show();                    }
              }
          })            
      }    
  };
  </code></pre>
  
  <p>Does <code>var check = false</code> means that everything is written after it (the for loop in this case) is false?</p>
  
  <p>Does <code>!check</code> means that <code>var check = false</code> isn't true, i.e. <code>check === true</code>?
  If so, why should I specify <code>check = true</code> inside the for loop? Isn't <code>check = true</code> the same as <code>!check</code>? In other terms, what is the <code>check</code> telling me?</p>