for loop in function calling another function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zimitry
    New Member
    • May 2007
    • 11

    for loop in function calling another function

    Hi

    i currently have this code for an assignment i'm working my way through

    [code=javascript]
    var flightTimes = new Array ('07:15', '08:05', '08:30', '09:30', '09:50', '10:00', '10:20', '10:45', '11:00', '12:00', '13:00', '13:05', '13:15', '13:35', '14:10', '15:00', '16:00', '16:40', '16:55', '17:00', '19:30', '21:00', '22:00', '23:20', '23:59');
    var flightDestinati ons = new Array ('Arkham', 'Hensonville', 'Stepford', 'Maycomb', 'Bay Port', 'Denton', 'Waldon City', 'Denton', 'Denton', 'Waldon City', 'Arkham', 'Maycomb', 'Bay Port', 'Stepford', 'Denton', 'Maycomb', 'Waldon City', 'Hensonville', 'Denton', 'Denton', 'Stepford', 'Bay Port', 'Hensonville', 'Waldon City', 'Arkham');
    var flightOperators = new Array ('Miskatonic', 'SuperSwift', 'Aalto', 'DynaJet', 'Oceanic', 'Aalto', 'Aalto', 'MidSouthCentra l Air', 'SuperSwift', 'SuperSwift', 'Miskatonic', 'DynaJet', 'Miskatonic', 'SuperSwift', 'MidSouthCentra l Air', 'Aalto', 'SuperSwift', 'Aalto', 'MidSouthCentra l Air', 'Aalto', 'DynaJet', 'Oceanic', 'SuperSwift', 'Aalto', 'Miskatonic');
    var flightFares = new Array (300, 100, 150, 75, 250, 150, 75, 125, 100, 25, 600, 75, 400, 250, 300, 250, 150, 200, 125, 150, 75, 100, 100, 50, 300);

    function displayMessage( aMessage)
    {
    document.getEle mentById('theFl ights').innerHT ML = aMessage;
    }

    function showTimetable()
    {

    for (var i = 0; i <= flightTimes.len gth; i = i + 1)

    {
    if (i <= flightTimes.len gth - 1)
    {
    displayMessage( (i + 1) + ' ' + flightTimes[i] + ' to ' + flightDestinati ons[i] + ' operated by ' + flightOperators[i] + '. $' + flightFares[i] + '<BR>');
    }
    }


    /*for (var i = 0; i <= flightTimes.len gth; i = i + 1)

    {
    if (i <= flightTimes.len gth - 1)
    {
    document.write( (i + 1) + ' ' + flightTimes[i] + ' to ' + flightDestinati ons[i] + ' operated by ' + flightOperators[i] + '. $' + flightFares[i] + '<BR>');
    }
    }*/

    }

    [/code]

    the trouble i'm having is getting the displayMessage to write down all of the Array's in a <DIV>

    it works fine when you do document.write but when you do display message it works it way through the arrays only displaying the last one?

    i can't really think of which way to do it if i'm doing it correct in the first place?

    any help is much appreciated

    z
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by zimitry
    the trouble i'm having is getting the displayMessage to write down all of the Array's in a <DIV>

    it works fine when you do document.write but when you do display message it works it way through the arrays only displaying the last one?

    i can't really think of which way to do it if i'm doing it correct in the first place?

    any help is much appreciated

    z
    Try
    Code:
    document.getElementById('theFlights').innerHTML += aMessage;

    Comment

    • zimitry
      New Member
      • May 2007
      • 11

      #3
      Cool that worked!

      i did have a workaround of this

      [code=javascript]
      var myString1 = ('1 ' + flightTimes[0] + ' to ' + flightDestinati ons[0] + ' operated by ' + flightOperators[0] + '. $' + flightFares[0] + '<BR>' +
      '2 ' + flightTimes[1] + ' to ' + flightDestinati ons[1] + ' operated by ' + flightOperators[1] + '. $' + flightFares[1] + '<BR>' +
      '3 ' + flightTimes[2] + ' to ' + flightDestinati ons[2] + ' operated by ' + flightOperators[2] + '. $' + flightFares[2] + '<BR>' +
      '4 ' + flightTimes[3] + ' to ' + flightDestinati ons[3] + ' operated by ' + flightOperators[3] + '. $' + flightFares[3] + '<BR>' +
      '5 ' + flightTimes[4] + ' to ' + flightDestinati ons[4] + ' operated by ' + flightOperators[4] + '. $' + flightFares[4] + '<BR>' );

      var myString2 = ('6 ' + flightTimes[5] + ' to ' + flightDestinati ons[5] + ' operated by ' + flightOperators[5] + '. $' + flightFares[5] + '<BR>' +
      '7 ' + flightTimes[6] + ' to ' + flightDestinati ons[6] + ' operated by ' + flightOperators[6] + '. $' + flightFares[6] + '<BR>' +
      '8 ' + flightTimes[7] + ' to ' + flightDestinati ons[7] + ' operated by ' + flightOperators[7] + '. $' + flightFares[7] + '<BR>' +
      '9 ' + flightTimes[8] + ' to ' + flightDestinati ons[8] + ' operated by ' + flightOperators[8] + '. $' + flightFares[8] + '<BR>' +
      '10 ' + flightTimes[9] + ' to ' + flightDestinati ons[9] + ' operated by ' + flightOperators[9] + '. $' + flightFares[9] + '<BR>' );

      var myString3 = ('11 ' + flightTimes[10] + ' to ' + flightDestinati ons[10] + ' operated by ' + flightOperators[10] + '. $' + flightFares[10] + '<BR>' +
      '12 ' + flightTimes[11] + ' to ' + flightDestinati ons[11] + ' operated by ' + flightOperators[11] + '. $' + flightFares[11] + '<BR>' +
      '13 ' + flightTimes[12] + ' to ' + flightDestinati ons[12] + ' operated by ' + flightOperators[12] + '. $' + flightFares[12] + '<BR>' +
      '14 ' + flightTimes[13] + ' to ' + flightDestinati ons[13] + ' operated by ' + flightOperators[13] + '. $' + flightFares[13] + '<BR>' +
      '15 ' + flightTimes[14] + ' to ' + flightDestinati ons[14] + ' operated by ' + flightOperators[14] + '. $' + flightFares[14] + '<BR>' );

      var myString4 = ('16 ' + flightTimes[15] + ' to ' + flightDestinati ons[15] + ' operated by ' + flightOperators[15] + '. $' + flightFares[15] + '<BR>' +
      '17 ' + flightTimes[16] + ' to ' + flightDestinati ons[16] + ' operated by ' + flightOperators[16] + '. $' + flightFares[16] + '<BR>' +
      '18 ' + flightTimes[17] + ' to ' + flightDestinati ons[17] + ' operated by ' + flightOperators[17] + '. $' + flightFares[17] + '<BR>' +
      '19 ' + flightTimes[18] + ' to ' + flightDestinati ons[18] + ' operated by ' + flightOperators[18] + '. $' + flightFares[18] + '<BR>' +
      '20 ' + flightTimes[19] + ' to ' + flightDestinati ons[19] + ' operated by ' + flightOperators[19] + '. $' + flightFares[19] + '<BR>' );

      var myString5 = ('21 ' + flightTimes[20] + ' to ' + flightDestinati ons[20] + ' operated by ' + flightOperators[20] + '. $' + flightFares[20] + '<BR>' +
      '22 ' + flightTimes[21] + ' to ' + flightDestinati ons[21] + ' operated by ' + flightOperators[21] + '. $' + flightFares[21] + '<BR>' +
      '23 ' + flightTimes[22] + ' to ' + flightDestinati ons[22] + ' operated by ' + flightOperators[22] + '. $' + flightFares[22] + '<BR>' +
      '24 ' + flightTimes[23] + ' to ' + flightDestinati ons[23] + ' operated by ' + flightOperators[23] + '. $' + flightFares[23] + '<BR>' +
      '25 ' + flightTimes[24] + ' to ' + flightDestinati ons[24] + ' operated by ' + flightOperators[24] + '. $' + flightFares[24] + '<BR>' );

      displayMessage( myString1 + '<BR>' + myString2 + '<BR>' + myString3 + '<BR>' + myString4 + '<BR>' + myString5);
      [/code]

      is there anything i could insert to put a gap every 5 lines? ie

      [code=javascript]
      if (flightTimes[i] == 5)
      {
      <BR>
      }
      [/code]

      Comment

      • zimitry
        New Member
        • May 2007
        • 11

        #4
        on Adding the + sign to the code everything i click a link button the text never clears the screen it just keeps adding to it? is this right

        z

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by zimitry
          on Adding the + sign to the code everything i click a link button the text never clears the screen it just keeps adding to it? is this right
          Yup. If you want to clear the text from it, put a line similar to this one before your loop executes:

          [code=javascript]
          var $theFlights = document.getEle mentById('theFl ights');
          while($theFligh ts.firstChild)
          $theFlights.rem oveChild($theFl ights.firstChil d);
          [/code]

          Comment

          • zimitry
            New Member
            • May 2007
            • 11

            #6
            Is there no way in which this can be done without chaing the actual function as it's used for other tasks?

            z

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by zimitry
              Is there no way in which this can be done without chaing the actual function as it's used for other tasks?

              z
              You mean showTimetable() ? Does it matter if "theFlights " div is cleared before you show the timetable?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by zimitry
                is there anything i could insert to put a gap every 5 lines? ie

                [code=javascript]
                if (flightTimes[i] == 5)
                {
                <BR>
                }
                [/code]
                [CODE=javascript]if (i % 5 == 0)[/CODE] % is the modulus operator. It gives the remainder.

                Comment

                • zimitry
                  New Member
                  • May 2007
                  • 11

                  #9
                  What i mean is the there no way in doing this without chaning the function displayMessage?

                  [code=javascript]

                  function displayMessage( aMessage)
                  {
                  document.getEle mentById('theFl ights').innerHT ML = aMessage;
                  }

                  [/code]

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Just define and call another function if you don't want to change the function.

                    Comment

                    Working...