You are on page 1of 6

Token Generator

Grade settings: Maximum grade: 100


Run: Yes Evaluate: Yes
Automatic grade: Yes Maximum memory used: 512 MiB Maximum number of processes:
10000
Objective:
To add scripting to existing web page and work with JavaScript basic activities. The basic
Javascript concepts like DOM, Date functions, String functions and Control Statements are
covered.

Problem Description:

The passport office wants to generate a unique token for all the applicants and hence it has
decided to create an application that will generate the token from the given data of the applicants.

Following are the files that contains code snippets.

HTML for webpage (complete implementation is given for


index.html you). You only have to run this. No change needs to be done
in this file.
script.js Add your code in this file for the functions given.

Procedure to complete the exercise

1.    Required function with partially filled body is already available in the javascript file
Hint : Do NOT change the function names.
Fill the following functions with required code

tokenGen () On Clicking Generate Token button, this


function is invoked. Part of this function
implementation is given in code template. 

 
getToken(fname,phone,dob) This function calculates the age based on date
of birth (dob)

      if the age is greater than or equal to 18 then


generate the token by calling the newtoken
function

       and passing the firstname, phone and age


as arguments - newtoken(fname,phone,age);

       else specify token value as "Not eligible to


receive token"

       Return the token value to tokenGen


function
newtoken(fname,phone,age) 1. Verify the correctness of phone number. It
must have 10 numerical digits

       2. if phone number is valid then extract the


first 2 and the last 2 digits to make a number
and store it in phtoken else phtoken=0

       3. if fname (firstname) length is less than 2


then set fntoken=xy else set fntoken to the first
2 characters of fname

       4. Generate the final token which is a


combination of fntoken + (added value of
phtoken and age)

       5. Return the finally constructed token

Screenshot 1

Step 1: getToken() is called from the base function : tokenGen() (given in template)
Step 2: getToken() calculates age from dob as 42.
Step 3: Since 42>18,  newtoken() is called where phone number is validated.
Step 4: Since phone number has 10 digits, the first 2 and the last 2 digits are combined to
for : phtoken which is 9328
Step 5: If the length of the firstname is less than 2, set fntoken=xy, else set fntoken to the first
2 characters of fname. Here, fntoken = Ab
Step 6: fntoken + (added value of phtoken and age). Hence, Ab + (9328+42) which is Ab9370.
 
Screenshot 2

Screenshot 3

Screenshot 4
Screenshot 5

Solution:

index.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="ISO-8859-1">
5 <title>Token Generator</title>
6 <script src="script.js" lang="text/javascript"></script>
7 </head>
8 <body>
9 FirstName <input id=fname type=text> <br><br>
10 Phone Number <input id="phone" type="number"><br><br>
11 Date of Birth <input id="dob" type="date"><br><br>
12 <button id="tokengen" onclick="tokenGen()">Generate
Token</button><br><br>
13 <div id="result"></div>
14 </body>
15 </html>
script.js
1
2
3 function newtoken(fname,phone,age)
4 {
5
6 /*
7 TODO -
8 1. verify the correctness of phone number . It must have 10
numerical digits
9 2. if phone number is valid then extract the first 2 and the last
2 digits to make a number and store it in phtoken else phtoken=0
10 3. if fname (firstname) length is less than 2 then set fntoken=xy
else set fntoken to the first 2 characters of fname
11 4. Generate the final token which is a combination of fntoken +
(added value of phtoken and age)
12 5. Return the finally constructed token
13
14 */let phoneString = String(phone);
15 let phtoken, fntoken;
16 if(phoneString.length === 10){
17 phtoken = phoneString.slice(0, 2) + phoneString.slice(-2);
18 }
19 else{
20 phtoken = 0;
21 }
22 if(fname.length >= 2){
23 fntoken = fname.slice(0, 2);
24 }
25 else{
26 fntoken = 'xy';
27 }
28 return "Your token Number is " + fntoken + String(Number(phtoken) +
Number(age));
29 }
30
31 function getToken(fname,phone,dob)
32 {
33
34 /*
35 TODO - Calculate the age based on date of birth (dob)
36 if the age is greater than or equal to 18 then generate the token
by calling the newtoken function
37 and passing the firstname, phone and age as arguments -
newtoken(fname,phone,age);
38 else specify token value as "Not eligible to receive token"
39 Return the token value to tokenGen function
40 */
41 var newDob = new Date(dob);
42 var month_diff = Date.now() - newDob.getTime();
43 var age_dt = new Date(month_diff);
44 var year = age_dt.getUTCFullYear();
45 var age = Math.abs(year - 1970);
46 if(age >=18){
47 return newtoken(fname, phone, age);
48 }
49 else{
50 return "Not eligible to receive token";
51 }
52 }
53
54 function tokenGen()
55 {
56 var fname=document.getElementById("fname").value;
57 var phone=document.getElementById("phone").value;
58 var dob=document.getElementById("dob").value;
59
60 if(fname.length!=0 && phone.length!=0 && dob.length!=0)
61 {
62 token=getToken(fname,phone,dob);
63 document.getElementById("result").innerHTML=token;
64 }
65 else
66 {
67 document.getElementById("result").innerHTML="Please enter all
details to generate token";
68 return "Please enter all details to generate token";
69 }
70 }
71

You might also like