For Loop II - Use of flags and boolean

Flags and Boolean in Loop are used when you want to display any message when no match is found.

Code Structure
var answer = prompt("Enter the city");
var cities = ["London", "Reading", "Manchester", "Liverpool", "Swansea"];
var matchFound = false;
for(var i = 0; i < cities.length; i++) {
 if(answer === cities[i]){
  matchFound = true;
  alert("You are in UK");
if(matchFound === false){
 alert("You are in rest of the country");

Share this

Related Posts