1 Replies - 1565 Views - Last Post: 13 July 2010 - 05:34 PM

#1 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Train Schedule Script

Posted 13 July 2010 - 10:28 AM

First off I think I'm learning from an outdated lesson site. This is the site.. I was wondering if anyone could give me any tips to shorten this code. It seems kinda long; doing some of the calculations and actions twice. I'd like to get it done with the knowledge I'm supposed to have in the lessons. So no functions, no arrays, no date function. I used a for loop just because I haven't used one yet in the exercises. I've stuck mostly to do...while and while loops.

Anyway any tips would be appreciated.

The following is the exercise:

The first train to London from my local train station arrives promptly at 5.33 in the morning and then a train arrives regularly every 19 minutes after that. However, the service stops fairly early: anyone arriving at the station after 10 p.m. has missed the last train.

1. Write a program which displays the times of all the trains to London.
2. Adapt the program so that it also counts the number of trains to London.
3. The journey to London takes exactly 34 minutes. Adapt the program so that it also gives the arrival times of all the trains.

My code is as follows:

Is there anything obviously wrong? It runs and gives dates properly. I know i could use Javascript's built in time object but I haven't learned that yet in the lessons. I'm sure this is the absolute long-hand script to accomplish the end result but I'm only using what I should know from the lessons I'm going through.

<html>
<head>
<title>London Train Schedule</title>
</head>
<body>
<script language="Javascript" type="text/javascript">
<!--
var depTime;
var arrivalTime;
var i = 19;
var trainNum;
var tt = 34;
var depHr = 5;
var depMin = 33;
var arHr;
var arMin;


document.write("<table border=\"1\"><thead><tr><th>Train No</th><th>Train Departure</th><th>Train Arrival</th></tr></thead><tbody>");

for (trainNum = 1; depHr < 22; trainNum++) {
	if (depMin < 10) {
		depMin = "0" + depMin;
	}
	if (depHr < 12) {
		depTime = depHr + ":" + depMin + "AM";
	}
	else {
		if (depHr > 12) {
			depTime = (depHr - 12) + ":" + depMin + "PM";
		}
		else
			depTime = depHr + ":" + depMin + "PM";
	}

	depMin = parseFloat(depMin);

	arHr = depHr;
	arMin = depMin + tt;

	if (arMin >= 60) {
		arHr++;
		arMin = arMin - 60;
	}

	if (arMin < 10) {
		arMin = "0" + arMin;
	}

	if (arHr < 12) {
		arrivalTime = arHr + ":" + arMin + "AM";
	}
	else {
		if (arHr > 12) {
			arrivalTime = (arHr - 12) + ":" + arMin + "PM";
		}
		else
			arrivalTime = arHr + ":" + arMin + "PM";
	}
	
	document.write("<tr><td>" + trainNum + "</td><td>" + depTime + "</td><td>" + arrivalTime + "</td></tr>");

	depMin = depMin + i;

	if (depMin >= 60) {
		depHr++;
		depMin = depMin - 60;
	}
}
document.write("</tbody></table>");

//-->
</script>
</body>
</html>


This post has been edited by monkadelicd: 13 July 2010 - 03:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Train Schedule Script

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Train Schedule Script

Posted 13 July 2010 - 05:34 PM

I substituted a "do..while" for your "for..loop" logic,
but until you get to use functions there will always
be redundant and less efficient code.

<html>
<head>
<title>London Train Schedule</title>
</head>
<body>
<script type="text/javascript"> <!-- archaic form: language="Javascript" -->
// From: http://www.dreamincode.net/forums/topic/181395-train-schedule-script/

var depTime;		// departing time
var arrivalTime;	// arrival time
var i = 19;			// time between departures
var trainNum = 0;		// train number departing
var tt = 34;		// total trip time
var depHr = 5;		// first departing time hour
var depMin = 33;	// first departing time minute
var arHr;			// arrival hour
var arMin;			// arrival minute

var str = '';
str += '<table border="1">';
str += '<thead>';
str += '<tr><th>Train No</th><th>Train Departure</th><th>Train Arrival</th></tr>';
str += '</thead>';
str += '<tbody>';

// for (trainNum = 1; depHr < 22; trainNum++) { // for loop starts
  do {     // do..loop starts
    trainNum++;  // note, initialized to zero above

	if (depMin < 10) { depMin = "0" + depMin; }
	if (depHr < 12) { depTime = depHr + ":" + depMin + "AM"; } 
	           else { if (depHr > 12) { depTime = (depHr - 12) + ":" + depMin + "PM"; }
		                         else { depTime = depHr + ":" + depMin + "PM"; }
		            }
	depMin = parseFloat(depMin);
	arHr = depHr;
	arMin = depMin + tt;
	if (arMin >= 60) { arHr++; arMin = arMin - 60; }
	if (arMin < 10) { arMin = "0" + arMin; }
	if (arHr < 12) { arrivalTime = arHr + ":" + arMin + "AM"; }
    	      else { if (arHr > 12) { arrivalTime = (arHr - 12) + ":" + arMin + "PM"; }
		                       else { arrivalTime = arHr + ":" + arMin + "PM"; }
	               }
	str += "<tr><td>" + trainNum + "</td>";
	str += "<td>" + depTime + "</td>";
	str += "<td>" + arrivalTime + "</td></tr>";
	depMin = depMin + i;
	if (depMin >= 60) { depHr++; depMin = depMin - 60; }
  } while (depHr < 22);
// } // for..loop ends

  str += "</tbody></table>";
  document.write(str);
</script>
</body>
</html>


Good Luck! :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1