473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

for loop in function calling another function

11
Hi

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

Expand|Select|Wrap|Line Numbers
  1. 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');
  2.        var flightDestinations = 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');
  3.        var flightOperators = new Array ('Miskatonic', 'SuperSwift', 'Aalto', 'DynaJet', 'Oceanic', 'Aalto', 'Aalto', 'MidSouthCentral Air', 'SuperSwift', 'SuperSwift', 'Miskatonic', 'DynaJet', 'Miskatonic', 'SuperSwift', 'MidSouthCentral Air', 'Aalto', 'SuperSwift', 'Aalto', 'MidSouthCentral Air', 'Aalto', 'DynaJet', 'Oceanic', 'SuperSwift', 'Aalto', 'Miskatonic');
  4. 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);
  5.  
  6. function displayMessage(aMessage)
  7.        {
  8.                  document.getElementById('theFlights').innerHTML = aMessage;
  9.        }
  10.  
  11. function showTimetable()
  12.        {
  13.  
  14.            for (var i = 0; i <= flightTimes.length; i = i + 1)
  15.  
  16.                {
  17.                    if (i <= flightTimes.length - 1)
  18.                         {
  19.                         displayMessage((i + 1) + ' ' + flightTimes[i] + ' to ' + flightDestinations[i] + ' operated by ' + flightOperators[i] + '. $' + flightFares[i] + '<BR>');
  20.                         }
  21.                }
  22.  
  23.  
  24.            /*for (var i = 0; i <= flightTimes.length; i = i + 1)
  25.  
  26.                {
  27.                    if (i <= flightTimes.length - 1)
  28.                         {
  29.                         document.write((i + 1) + ' ' + flightTimes[i] + ' to ' + flightDestinations[i] + ' operated by ' + flightOperators[i] + '. $' + flightFares[i] + '<BR>');
  30.                         }
  31.                }*/
  32.  
  33.         }
  34.  
  35.  
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
May 26 '07 #1
9 1778
Logician
210 100+
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
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('theFlights').innerHTML += aMessage;
May 26 '07 #2
zimitry
11
Cool that worked!

i did have a workaround of this

Expand|Select|Wrap|Line Numbers
  1. var myString1 = ('1 ' + flightTimes[0] + ' to ' + flightDestinations[0] + ' operated by ' + flightOperators[0] + '. $' + flightFares[0] + '<BR>' + 
  2.            '2 ' + flightTimes[1] + ' to ' + flightDestinations[1] + ' operated by ' + flightOperators[1] + '. $' + flightFares[1] + '<BR>' + 
  3.            '3 ' + flightTimes[2] + ' to ' + flightDestinations[2] + ' operated by ' + flightOperators[2] + '. $' + flightFares[2] + '<BR>' +
  4.            '4 ' + flightTimes[3] + ' to ' + flightDestinations[3] + ' operated by ' + flightOperators[3] + '. $' + flightFares[3] + '<BR>' +
  5.            '5 ' + flightTimes[4] + ' to ' + flightDestinations[4] + ' operated by ' + flightOperators[4] + '. $' + flightFares[4] + '<BR>' );
  6.  
  7.            var myString2 = ('6 ' + flightTimes[5] + ' to ' + flightDestinations[5] + ' operated by ' + flightOperators[5] + '. $' + flightFares[5] + '<BR>' + 
  8.            '7 ' + flightTimes[6] + ' to ' + flightDestinations[6] + ' operated by ' + flightOperators[6] + '. $' + flightFares[6] + '<BR>' + 
  9.            '8 ' + flightTimes[7] + ' to ' + flightDestinations[7] + ' operated by ' + flightOperators[7] + '. $' + flightFares[7] + '<BR>' +
  10.            '9 ' + flightTimes[8] + ' to ' + flightDestinations[8] + ' operated by ' + flightOperators[8] + '. $' + flightFares[8] + '<BR>' +
  11.            '10 ' + flightTimes[9] + ' to ' + flightDestinations[9] + ' operated by ' + flightOperators[9] + '. $' + flightFares[9] + '<BR>' );
  12.  
  13.            var myString3 = ('11 ' + flightTimes[10] + ' to ' + flightDestinations[10] + ' operated by ' + flightOperators[10] + '. $' + flightFares[10] + '<BR>' + 
  14.            '12 ' + flightTimes[11] + ' to ' + flightDestinations[11] + ' operated by ' + flightOperators[11] + '. $' + flightFares[11] + '<BR>' + 
  15.            '13 ' + flightTimes[12] + ' to ' + flightDestinations[12] + ' operated by ' + flightOperators[12] + '. $' + flightFares[12] + '<BR>' +
  16.            '14 ' + flightTimes[13] + ' to ' + flightDestinations[13] + ' operated by ' + flightOperators[13] + '. $' + flightFares[13] + '<BR>' +
  17.            '15 ' + flightTimes[14] + ' to ' + flightDestinations[14] + ' operated by ' + flightOperators[14] + '. $' + flightFares[14] + '<BR>' );
  18.  
  19.            var myString4 = ('16 ' + flightTimes[15] + ' to ' + flightDestinations[15] + ' operated by ' + flightOperators[15] + '. $' + flightFares[15] + '<BR>' + 
  20.            '17 ' + flightTimes[16] + ' to ' + flightDestinations[16] + ' operated by ' + flightOperators[16] + '. $' + flightFares[16] + '<BR>' + 
  21.            '18 ' + flightTimes[17] + ' to ' + flightDestinations[17] + ' operated by ' + flightOperators[17] + '. $' + flightFares[17] + '<BR>' +
  22.            '19 ' + flightTimes[18] + ' to ' + flightDestinations[18] + ' operated by ' + flightOperators[18] + '. $' + flightFares[18] + '<BR>' +
  23.            '20 ' + flightTimes[19] + ' to ' + flightDestinations[19] + ' operated by ' + flightOperators[19] + '. $' + flightFares[19] + '<BR>' );
  24.  
  25.            var myString5 = ('21 ' + flightTimes[20] + ' to ' + flightDestinations[20] + ' operated by ' + flightOperators[20] + '. $' + flightFares[20] + '<BR>' + 
  26.            '22 ' + flightTimes[21] + ' to ' + flightDestinations[21] + ' operated by ' + flightOperators[21] + '. $' + flightFares[21] + '<BR>' + 
  27.            '23 ' + flightTimes[22] + ' to ' + flightDestinations[22] + ' operated by ' + flightOperators[22] + '. $' + flightFares[22] + '<BR>' +
  28.            '24 ' + flightTimes[23] + ' to ' + flightDestinations[23] + ' operated by ' + flightOperators[23] + '. $' + flightFares[23] + '<BR>' +
  29.            '25 ' + flightTimes[24] + ' to ' + flightDestinations[24] + ' operated by ' + flightOperators[24] + '. $' + flightFares[24] + '<BR>' );
  30.  
  31.           displayMessage(myString1 + '<BR>' + myString2 + '<BR>' + myString3 + '<BR>' + myString4 + '<BR>' + myString5);
  32.  
is there anything i could insert to put a gap every 5 lines? ie

Expand|Select|Wrap|Line Numbers
  1. if (flightTimes[i] == 5)
  2. {
  3. <BR>
  4. }
  5.  
May 26 '07 #3
zimitry
11
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
May 26 '07 #4
pbmods
5,821 Expert 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. var $theFlights = document.getElementById('theFlights');
  2. while($theFlights.firstChild)
  3.     $theFlights.removeChild($theFlights.firstChild);
  4.  
May 27 '07 #5
zimitry
11
Is there no way in which this can be done without chaing the actual function as it's used for other tasks?

z
May 27 '07 #6
acoder
16,027 Expert Mod 8TB
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?
May 28 '07 #7
acoder
16,027 Expert Mod 8TB
is there anything i could insert to put a gap every 5 lines? ie

Expand|Select|Wrap|Line Numbers
  1. if (flightTimes[i] == 5)
  2. {
  3. <BR>
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. if (i % 5 == 0)
% is the modulus operator. It gives the remainder.
May 28 '07 #8
zimitry
11
What i mean is the there no way in doing this without chaning the function displayMessage?

Expand|Select|Wrap|Line Numbers
  1.  
  2. function displayMessage(aMessage)
  3.        {
  4.                  document.getElementById('theFlights').innerHTML = aMessage;
  5.        }
  6.  
  7.  
May 28 '07 #9
acoder
16,027 Expert Mod 8TB
Just define and call another function if you don't want to change the function.
May 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
6
by: Ed Jay | last post by:
<disclaimer>New to js.</disclaimer> I have several pages, each with menues comprising checkboxes or radio boxes within the same form. I presently 'brute force' clear the buttons with individual...
7
by: gmou | last post by:
Dear group, I am building a translator from C++ into VB (and into C#). At the moment, I have a hard time figuring out the equivalent of a 'for' loop in VB. Given C++ code: for( int i=0;...
3
by: MicroMoth | last post by:
Hi, I'm trying to call a Javascript function within a foreach loop. I am loop over a series of users and I want to call the JS function which opens a new window, passing in the user id to each...
20
by: hufaunder | last post by:
I have 16-bit data that I want to display. In order to display it I compress a certain range of the input data into 8 bit (I need control over this). All seems to work ok except that it is dead...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.