File tree Expand file tree Collapse file tree 3 files changed +113
-0
lines changed
Expand file tree Collapse file tree 3 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < link rel ="stylesheet " href ="styles.css ">
5+ < meta charset ="UTF-8 ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < title > Age Calculator</ title >
8+ </ head >
9+ < body >
10+ < main >
11+ < h1 id ="title "> Age Calculator</ h1 >
12+ < label for ="dob "> Enter you date of birth</ label >
13+ < input type ="date " name ="" id ="dob ">
14+ < button id ="calculate "> Calculate Age</ button >
15+ < h3 id ="output "> </ h3 >
16+
17+ </ main >
18+ < script src ="script.js "> </ script >
19+ </ body >
20+ </ html >
Original file line number Diff line number Diff line change 1+ const dob = document . getElementById ( "dob" ) ;
2+ const calculate = document . getElementById ( "calculate" ) ;
3+ const output = document . getElementById ( "output" ) ;
4+
5+
6+ const calculateAge = ( ) => {
7+ const birthdayValue = dob . value ;
8+ if ( birthdayValue === "" ) {
9+ alert ( "Please select your birthday" )
10+ } else {
11+ const age = getAge ( birthdayValue ) ;
12+ output . innerText = `Your age is ${ age } ${ age > 1 ? "years" : "year" } old`
13+ }
14+ } ;
15+
16+ const getAge = ( birthdayValue ) => {
17+ const currentDate = new Date ( ) ;
18+ const birthdayDate = new Date ( birthdayValue ) ;
19+ let age = currentDate . getFullYear ( ) - birthdayDate . getFullYear ( ) ;
20+ const month = currentDate . getMonth ( ) - birthdayDate . getMonth ( ) ;
21+
22+ if (
23+ month < 0 ||
24+ ( month === 0 && currentDate . getDate ( ) < birthdayDate . getDate ( ) )
25+ ) {
26+ age -- ;
27+ }
28+ return age ;
29+ }
30+
31+ calculate . addEventListener ( "click" , calculateAge ) ;
32+
Original file line number Diff line number Diff line change 1+ * {
2+ margin : 0 ;
3+ padding : 0 ;
4+ }
5+
6+ body {
7+ background-color : # f2f2f2 ;
8+ font-family : "Helvetica" , sans-serif;
9+ display : flex;
10+ align-items : center;
11+ justify-content : center;
12+ }
13+
14+ main {
15+ max-width : 40% ;
16+ width : 400px ;
17+ margin-top : 100px ;
18+ background-color : # ffffff ;
19+ box-shadow : 0px 0px 10px rgba (0 , 0 , 0 , 0.8 );
20+ padding : 20px 100px ;
21+ display : flex;
22+ border-radius : 10px ;
23+ flex-direction : column;
24+ }
25+
26+ h1 {
27+
28+ padding : 1rem ;
29+ text-align : center;
30+ }
31+
32+ label {
33+ padding : 0.6rem ;
34+ text-align : center;
35+ font-size : 1rem ;
36+ font-weight : 700 ;
37+ }
38+ input {
39+ font-size : 1rem ;
40+
41+ }
42+
43+ button {
44+ margin : 1rem auto;
45+ padding : 10px ;
46+ background-color : # 007bff ;
47+ border : none;
48+ border-radius : 5px ;
49+ color : # fff ;
50+ cursor : pointer;
51+ }
52+
53+ button : hover {
54+ background-color : # 007baa ;
55+ }
56+
57+ h3 {
58+
59+ padding : 10px ;
60+ text-align : center;
61+ }
You can’t perform that action at this time.
0 commit comments