mardi 2 juillet 2019

How to set a button to execute a for loop?

My code starts on first item in the first array and iterate through the second array. If the first item in the first array is the same as the item in the second array the value is printed to the console. So in this snippet Nandos and KFC should print to console.

Then once the first item in the first array has been compared to all of the items in the second array we would move to the second item in the first array and compare it to all the items in the second array.

I want to execute the for loop once the button is clicked.

I tried to make the for loop a function and to execute the function once the button was clicked but nothing happened.

<button id="search">Search</button>

var restaurantsOne = ["Nandos", "King Rooster", "Chick-Fil-A", "Dominos", "KFC"];
var restaurantsTwo = ["Nandos","MacDonalds","Burger King","KFC","Pizza Hut"];

  for (var i=0; i<=restaurantsOne.length; i++) {
    for (var j=0; j<=restaurantsTwo.length; j++){
      if (restaurantsOne[i] == restaurantsTwo[j]){
        console.log(restaurantsOne[i]);
      }
    }
  }

I would like the for loop to execute once the button was clicked

Aucun commentaire:

Enregistrer un commentaire