File tree 3 files changed +114
-0
lines changed
3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < title > Age Calculator</ title >
8
+ < link rel ="stylesheet " href ="style.css ">
9
+ </ head >
10
+ < body >
11
+ < div class ="container ">
12
+ < h1 > Age Calculator</ h1 >
13
+ < div class ="form ">
14
+ < label for ="birthday "> Enter your date of birth:</ label >
15
+ < input type ="date " id ="birthday " name ="birthday ">
16
+ < button id ="btn "> Calculate Age</ button >
17
+ < p id ="result "> Your age is ... years.</ p >
18
+ </ div >
19
+ </ div >
20
+
21
+ < script src ="script.js "> </ script >
22
+ </ body >
23
+ </ html >
Original file line number Diff line number Diff line change
1
+ const btnElement = document . getElementById ( "btn" ) ;
2
+ const birthdayElement = document . getElementById ( "birthday" ) ;
3
+ const resultElement = document . getElementById ( "result" ) ;
4
+
5
+ function calculateAge ( ) {
6
+ // console.log("Clicked!");
7
+ const birthdayValue = birthdayElement . value ;
8
+ // console.log(birthdayValue);
9
+
10
+ if ( birthdayValue === "" ) {
11
+ alert ( "Please, enter your birthdate." ) ;
12
+ } else {
13
+ const age = getAge ( birthdayValue ) ;
14
+ // console.log(age);
15
+ resultElement . innerText = `Your age is ${ age } ${ age > 1 ? "years" : "year" } .` ;
16
+ }
17
+ }
18
+
19
+ function getAge ( birthdayValue ) {
20
+ const currentDate = new Date ( ) ;
21
+ const birthDate = new Date ( birthdayValue ) ;
22
+ // console.log(currentDate.getFullYear());
23
+ // console.log(birthDate.getFullYear());
24
+ let age = currentDate . getFullYear ( ) - birthDate . getFullYear ( ) ;
25
+ // console.log(age);
26
+ const month = currentDate . getMonth ( ) - birthDate . getMonth ( ) ;
27
+ // console.log(month);
28
+ if ( month < 0 || ( month === 0 && currentDate . getDate ( ) < birthDate . getDate ( ) ) ) {
29
+ age -- ;
30
+ // console.log(age);
31
+ }
32
+ return age ;
33
+ }
34
+
35
+ btnElement . addEventListener ( "click" , calculateAge )
Original file line number Diff line number Diff line change
1
+ body {
2
+ margin : 0 ;
3
+ padding : 20px ;
4
+ font-family : Arial, Helvetica, sans-serif;
5
+ background-color : # f7f7f7 ;
6
+ /* background-color:#0062cc; */
7
+ }
8
+ .container {
9
+ background-color : white;
10
+ box-shadow : 0 0 10px rgba (0 , 0 , 0 , 0.2 );
11
+ padding : 20px ;
12
+ max-width : 600px ;
13
+ margin : 0 auto;
14
+ margin-top : 50px ;
15
+ border-radius : 5px ;
16
+ }
17
+ h1 {
18
+ font-size : 36px ;
19
+ text-align : center;
20
+ margin-top : 0 ;
21
+ margin-bottom : 20px ;
22
+ }
23
+ .form {
24
+ display : flex;
25
+ flex-direction : column;
26
+ align-items : center;
27
+ }
28
+ label {
29
+ font-weight : bold;
30
+ margin-bottom : 10px ;
31
+ }
32
+ input {
33
+ padding : 5px ;
34
+ border : 1px solid # ccc ;
35
+ border-radius : 5px ;
36
+ width : 100% ;
37
+ max-width : 250px ;
38
+ }
39
+ button {
40
+ background-color : # 007bff ;
41
+ color : white;
42
+ margin-top : 10px ;
43
+ padding : 10px 30px ;
44
+ border : none;
45
+ border-radius : 5px ;
46
+ cursor : pointer;
47
+ transition : background-color 0.3s ease;
48
+ }
49
+ button : hover {
50
+ background-color : # 0062cc ;
51
+ }
52
+ # result {
53
+ margin-top : 20px ;
54
+ font-size : 24px ;
55
+ font-weight : bold;
56
+ }
You can’t perform that action at this time.
0 commit comments