...

View Full Version : Can't figure out this for loop.



frankenstein
09-05-2010, 11:02 PM
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);
}



}

wildreason
09-05-2010, 11:06 PM
Right off the bat, your for declaration should read



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.

frankenstein
09-05-2010, 11:13 PM
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?

wildreason
09-05-2010, 11:51 PM
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.

frankenstein
09-06-2010, 05:57 PM
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.

Philip M
09-06-2010, 06:00 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum