File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Source-Code/AgeCalculator Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
2
+ const currDate = document . getElementById ( "currDate" ) ;
3
+ const dateOfBirth = document . querySelector ( "#DOB" ) ;
4
+ const calcAgeButton = document . getElementById ( "CalcAge" ) ;
5
+ const displayAge = document . getElementById ( "displayAge" ) ;
6
+ const ageText = document . getElementById ( "age" ) ;
7
+ const today = new Date ( ) ;
8
+
9
+ currDate . innerText = `Today's Date is: ${ today . toLocaleDateString ( "en-US" ) } ` ;
10
+
11
+ calcAgeButton . addEventListener ( "click" , ( ) => {
12
+ const birthDate = new Date ( dateOfBirth . value ) ;
13
+ let age = today . getFullYear ( ) - birthDate . getFullYear ( ) ;
14
+ const monthDifference = today . getMonth ( ) - birthDate . getMonth ( ) ;
15
+
16
+ if (
17
+ monthDifference < 0 ||
18
+ ( monthDifference === 0 && today . getDate ( ) < birthDate . getDate ( ) )
19
+ ) {
20
+ age -- ;
21
+ }
22
+
23
+ displayAge . style . visibility = "visible" ;
24
+ ageText . innerText = `You are ${ age } years old.` ;
25
+ } ) ;
26
+ } ) ;
You can’t perform that action at this time.
0 commit comments