Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can't figure out this for loop.

    Here is my code:

    I have written a for loop for the 'showTimetable()' function.

    It should loop through the 4 arrays listed below and produce a line like this:

    1 07:15 to Aarhus operated by Metar. £300. // this is the first line, numbered 1.

    It should loop through 25 times and display all the array elements, but it is only looping through once, and i can't figure out what the problem is.

    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 flightDestinations = new Array ('Aarhus', 'Hamburg', 'Siena', 'Malaga', 'Bayonne', 'Dublin', 'Warsaw', 'Dublin', 'Dublin', 'Warsaw', 'Aarhus', 'Malaga', 'Bayonne', 'Siena', 'Dublin', 'Malaga', 'Warsaw', 'Hamburg', 'Dublin', 'Dublin', 'Siena', 'Bayonne', 'Hamburg', 'Warsaw', 'Bayonne');
    var flightOperators = new Array ('Metar', 'SkySwift', 'Astral', 'Davian', 'OneJet', 'Astral', 'Astral', 'MiddleAir', 'SkySwift', 'SkySwift', 'Metar', 'Davian', 'Metar', 'SkySwift', 'MiddleAir', 'Astral', 'SkySwift', 'Astral', 'MiddleAir', 'Astral', 'Davian', 'OneJet', 'SkySwift', 'Astral', 'Metar');
    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);

    //
    // ***** DO *NOT* MODIFY THIS FUNCTION!!!! *****
    // A FUNCTION TO DISPLAY FLIGHT INFORMATION IN THE BROWSER WINDOW
    // ***** DO *NOT* MODIFY THIS FUNCTION!!!! *****
    //
    function displayMessage(aMessage)
    {
    document.getElementById('theFlights').innerHTML = aMessage;
    }


    //
    // A FUNCTION TO DISPLAY THE TIMETABLE OF FLIGHTS LEAVING Stanwick IMAGINARY AIRPORT




    function showTimetable()
    {

    for (var position = 0; position < flightTimes.length; position = position + 1 ) {

    position = ((position + 1 + " ") + flightTimes[position] + " to " + flightDestinations[position] + " operated by "
    + flightOperators[position]+ "." + " £" + flightFares[position] + "." + "<BR>");

    displayMessage(position);
    }



    }

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    St George, UT
    Posts
    138
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Right off the bat, your for declaration should read

    Code:
    for (var position = 0; position < flightTimes.length; position++ ) {
    And inside of the for loop, you shouldn't be modifying the variable position as it is what's used to control the iterations.

    Adding a string to a number turns it into a string, making it impossible to add or subtract from. The position + 1 + " " will always produce undesirable results.

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by wildreason View Post

    And inside of the for loop, you shouldn't be modifying the variable position as it is what's used to control the iterations.

    Adding a string to a number turns it into a string, making it impossible to add or subtract from. The position + 1 + " " will always produce undesirable results.
    What do you mean by this?

    I am very new.

    Do you mean i should assign a new variable, and use that instead?

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    St George, UT
    Posts
    138
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Yeah inside your for loop just change the name of the variable since it's already being used for the loop iteration. So positionMsg or something. Just so that it doesn't interfere with the for loop iterating.

  • #5
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by wildreason View Post
    Yeah inside your for loop just change the name of the variable since it's already being used for the loop iteration. So positionMsg or something. Just so that it doesn't interfere with the for loop iterating.
    function showTimetable()
    {

    for (var position = 0; position < flightTimes.length; position = position + 1 ) {

    var solution;
    solution = ((position + 1 + " ") + flightTimes[position] + " to " + flightDestinations[position] + " operated by "
    + flightOperators[position]+ "." + " £" + flightFares[position] + "." + "<BR>");


    displayMessage(solution);
    }

    }

    How about this?

    When i run this, it jumps to the very last elements in the arrays and the line:

    "25 23:59 to Bayonne operated by Metar. £300." is displayed.

    The number 25 is correct. So is the destination name. But the flightOperators array and the flightFares array, are still coming up as value 0.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Read Post#2.

    And inside of the for loop, you shouldn't be modifying the variable position as it is what's used to control the iterations.

    Adding a string to a number turns it into a string, making it impossible to add or subtract from. The position + 1 + " " will always produce undesirable results.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •