\Days-to-Go" is an interesting and useful feature in website design. It shows up the remaining

number of days (or even with hours, minutes, and seconds) towards an event. For example, at

anytime you visit the ocial website of Rio 2016 Olympic Game1, you will see such a feature

showing the number of days, hours, minutes and seconds towards the starting date of Rio 2016.

In this assignment, you are going to write a JavaScript program to implement the \Days-To-Go"

feature.

The Concept of Time in JavaScript

In JavaScript a time is de ned as a Date Object. Each date object stores its state as a time value,

which is a primitive number that encodes a date as milliseconds since 1 January 1970 00:00:00 UTC.

Thus, a date later than 1 January 1970 00:00:00 UTC will have a positive time value, whereas an

earlier date will have a negative time value. On the basis of the common timeline (which we all live

on), the distance between any two dates can be calculated using their time values in milliseconds.

Figure 1 illustrates the concept, where C is a date earlier than 1 January 1970 00:00:00 UTC, and

A and B are later with B being further than A.

1http://www.rio2016.com/en

Table 1: Constants

Description Value

Number of milliseconds in a day 1000*60*60*24

Number of milliseconds in an hour 1000*60*60

Number of milliseconds in a minute 1000*60

Number of milliseconds in a second 1000

Table 2: Variables

Description Initialising value description Type

Name of the event The name of Rio 2016 Olympic Game String

Year of the event The year of Rio 2016 Olympic Game Number

Month of the event The month of Rio 2016 Olympic Game Number

Day of the event The day of Rio 2016 Olympic Game Number

Calculation

1. Create a Date object for the date of the event by using the variables created previously.

 Send the object constructor the variables (not values) for year, month, and day of the

event;

 Mind the order of arguments sent to the constructor;

 Note that month numbers begin at 0 for January, 1 for February, and so on;

2. Create a Date object for the current time.

 No arguments need to be supplied to the constructor.

3. Calculate the di erence between the current time and the event time:

CSC1401 Assignment 1 Speci cation { Days-To-Go 3

 Use the getTime() member function to get a Date object's time value in milliseconds

 Deduct the time value of current time by using the value of event time.

4. Calculate the number of days to the event:

 Divide the time value di erence by the number of milliseconds in a day.

 Use the Math.

oor() function to reduce the result number to an integer.

5. Calculate the number of hours, minutes, and seconds in the remaining time value:

(a) Mod the time value di erence by the number of milliseconds in a day;

(b) Divide the mod result by the number of milliseconds in an hour;

(c) Use the Math.

oor() function to reduce the number to an integer for the number of

hours;

(d) Repeat Steps (a) to (c) to calculate the number of minutes and seconds. You may need

to update the calculating formula accordingly.

Presentation

Figure 2: Illustration of the Output

Figure 2 shows a sample output when running the \Days-

To-Go" program. Note that

- the information should be displayed using the alert()

function;

- wherever possible you should use variables in expres-

sions instead of explicit values (e.g., literals and num-

bers), for example, using the variable created for the

event name instead of a string value of \RIO 2016";

- the layout of output may vary depending on web

browsers.

Testing

Test your program by comparing its calculating results to the Days-To-Go feature on the ocial

website of Rio 2016 (http://www.rio2016.com/en). Note that due to di erent locality settings on

your computer and the Rio 2016 server, your calculation result could be slightly di erent from that

shown on the Rio 2016 website. For example, in a test when my program said \521 DAYS" to go,

the Rio 2016 website showed \522 DAYS" { Australia is in almost one day ahead of Rio de Janeiro.

Such a di erence is not an error and is acceptable.

Non-functional Requirements

Structure of the Source Code

 

  • 11 years ago
Java Script Days to GO
NOT RATED

Purchase the answer to view it

blurred-text
  • attachment
    6-4-15_63389_done.zip