matt2099
10/5/2018 - 1:50 PM

Looping Arrays

Loop through arrays

<DOCTYPE html>
<html>
  <head>
      <title> While and Do While Loops</title>
    </head>
<body>
  <script>

  //Three ways to declare arrays.
    var airlines = new Array();
    airlines[0] = "Delta";
    airlines[1] = "British Airways";
    airlines[2] = "American Airlines";
    airlines[3] = "Virgin";
    airlines[4] = "Qatar";
    airlines[5] = "Air Canada";
    airlines[6] = "Easyjet";
    airlines[6] = "Ryanair";
    airlines[6] = "Jet2";



    function output()
    {
      var arrayLength = airlines.length //count length of array at the beginning of function. Best not to put into the for loop to improve website performance.
      for(var i=0; i < arrayLength; i++) // set variable i to 0, run loop while i is less than the array length, add 1 to i once run
      {
        document.getElementById('output').innerHTML += airlines[i] + "<br/>";

      }
    }


  </script>
  <p id= "output"></p>
  <button onclick="output()">output</button>
</body>
</html>