Reference no: EM132349874
Assignment - Specification
Chinese Zodiac
Goals and Topics
The assignment problem is straightforward. All necessary details have been supplied. The solution of the problem will be straight line code which will use the programming concepts and strategies covered in Workshops 1-3. The subgoals are:
- Understanding values, variables constants, arrays, objects, operations, and the use of functions
- Translating simple design into JavaScript code
- The mechanism of editing, interpreting, building, running, and testing a program
- Commenting source code
- Becoming confident and comfortable with programming in small scale problem solving
Your task will be to write a program to accept user inputs for the date of birth, calculate the number of days the user has been alive, and give the corresponding Chinese Zodiac sign.
Inputs
The following inputs should be declared as variables and initialised at the beginning of your pro- gram:
- The year of birth (a valid 4 digit year);
- The month of birth (a valid integer number from 1 to 12, representing January to December);
- The date (in the month) of birth (an integer number from 1 to 31, representing a valid date of the month).
Code has been provided to handle the following tasks1:
- to receive inputs from the user;
- to convert the user inputs (String) to the proper type (Number).
//Accept user inputs for DOB and convert them into Number. The default value is set as 31/12/2016.
parseInt(prompt('Enter the year of birth as a 4 digit integer', '2016')); parseInt(prompt('Enter the month of birth as an integer, ranging from 1 to 12', '12')); parseInt(prompt('Enter the date of birth as an integer, ranging from 1 to 31', '31'));
Such statements will return the user inputs in proper Number type. You need to create some variables to store such returned results for further calculations in the program. In this assignment, we assume that the user inputs are always valid, e.g., the value entered for month is an integer number within the range of 1 - 12, etc. You are not required to validate the values input by the user.
Functional Requirements
Constants and Variables
Within the script section, create constants and variables following professional conventions and initialise them with right values. Some constants have been suggested in the following table.
Table 1: Constants
Description
|
Value
|
Number of milliseconds in a day
|
1000*60*60*24
|
The cycle of Chinese Zodiac
|
12
|
The year starting a Chinese Zodiac cycle
|
1024
|
The array to store 12 zodiac signs
|
'Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake',
'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig'
|
Calculation
1. Create a Date object for the date of birth based on the user input.
- Send the Date constructor the year, month number and date. Please notice that, in the
Date constructor, the number for month is starting from 0, rather than 1.
2. Create a Date object for the current time;
- No arguments supplied to the constructor
3. Calculate the user's age in milliseconds
- Dates are stored in milliseconds since 1 Jan 1970;
- Use the getTime() function to get a Date object's time in milliseconds
- Deduct the user's date of birth time from the current time
4. Calculate the user's age in days
- Divide the user's age in milliseconds by the number of milliseconds in a day.
- use the Math.floor() function to reduce the number to an integer.
5. Calculate the Chinese Zodiac
(a) Initialise an array object to host the String values of 12 Chinese Zodiac animal signs in correct order;
(b) In 1024 the Chinese Zodiac started a new cycle with the animal "Rat". Create a constant for the year of 1024;
(c) Use the minues operator to calculate the number of years between the user's year of birth and the constant created in (b);
(d) Use the mod operator to find the reminder after dividing the result of (c) by the constant created for the number of years in a Chinese Zodiac cycle;
(e) Use the reminder as index to find the correct animal sign in the Chinese Zodiac sign array created in (a).
Output
1. An alert statement to display the user's birthday by using the toDateString() function for Date object created for the user's date of birth. For example, "Your date of birth is Tue Apr 01 1980 ";
2. An alert statement to display how many days the user has been alive. For example, "You have been alive for 13489 days";
3. An alert statement to display the corresponding Chinese Zodiac animal sign. For example, "Your Chinese Zodiac sign is Monkey".
The following figures illustrate the results displayed regarding inputs of a user whose birthday is April 01, 1980, tested on March 07, 2017. Note that the layout of alert window and the font of text on different web browsers could be different, and they are not a concern in this assignment.
Non-functional Requirements
Structure of the Source Code
- All code should appear in the script section in the head of the HTML document.
- Do not write any code in the HTML body. Deliver all functionality by JavaScript.
- In the script order your code as follows:
(a) Constants;
(b) Variables and objects (declared and initialised);
(c) Other statements.
Comments
- You are required to add at least three comments to the source code.
- Do not comment every single line, instead, comment on blocks of code with a common purpose.
- Do not simply translate the syntax into English for comments, instead, describe the purpose of the code.
Attachment:- Chinese Zodiac Specification.rar