@@ -9,38 +9,51 @@ function calculateAge() {
9
9
alert ( "Please enter your birthday" ) ;
10
10
} else {
11
11
const age = getAge ( birthdayValue ) ;
12
- resultEl . innerText = `Your age is ${ age . years = 0 ?'' :age . years } ${ "years" } ${ age . months = 0 ?'' :age . months } ${ "months" } ${ age . days = 0 ?"" :age . days } ${ "days" } old` ;
12
+ const yeartext = age . years === 0 ? "" : age . years . toString ( ) + "years" ;
13
+ const monthtext = age . months === 0 ? "" : age . months . toString ( ) + "months" ;
14
+ const daytext =
15
+ age . days === 0
16
+ ? " "
17
+ : age . days . toString ( ) . concat ( age . days > 1
18
+ ? "days"
19
+ : "day" ) ;
20
+ // console.log(daytext)
21
+ // in Java script null,0,"", false and undefined all ==.Here use ===, otherwise
22
+ // always choose false, means the second option
23
+ resultEl . innerText = `Your age is ${ yeartext } ${ monthtext } ${ daytext } old` ;
13
24
}
14
25
}
15
26
// here cancel 0 display doesn'gitt work
27
+ // still got a bit issue, is 1 months display. will improve it later on.
28
+ // fixed. there are 4 string joining method. 1 use + 2.contact() 3 `{} 4 .join in array
16
29
17
30
function getAge ( birthdayValue ) {
18
-
19
- const today = new Date ( ) ;
20
- const birth = new Date ( birthdayValue ) ;
21
- let age = { } ;
22
-
23
- let yearDiff = today . getFullYear ( ) - birth . getFullYear ( ) ;
24
- let monthDiff = today . getMonth ( ) - birth . getMonth ( ) ;
25
- let dayDiff = today . getDate ( ) - birth . getDate ( ) ;
26
-
27
- if ( monthDiff < 0 || ( monthDiff === 0 && dayDiff < 0 ) ) {
28
- yearDiff -- ;
29
- if ( monthDiff < 0 ) {
30
- monthDiff += 12 ;
31
- }
32
- if ( dayDiff < 0 ) {
31
+ const today = new Date ( ) ;
32
+ const birth = new Date ( birthdayValue ) ;
33
+ let age = { } ;
34
+
35
+ let yearDiff = today . getFullYear ( ) - birth . getFullYear ( ) ;
36
+ let monthDiff = today . getMonth ( ) - birth . getMonth ( ) ;
37
+ let dayDiff = today . getDate ( ) - birth . getDate ( ) ;
38
+
39
+ if ( monthDiff < 0 || ( monthDiff === 0 && dayDiff < 0 ) ) {
40
+ yearDiff -- ;
41
+ if ( monthDiff < 0 ) {
42
+ monthDiff += 12 ;
43
+ }
44
+ if ( dayDiff < 0 ) {
33
45
const lastMonth = new Date ( today . getFullYear ( ) , today . getMonth ( ) - 1 , 0 ) ;
34
- dayDiff += lastMonth . getDate ( ) ; }
35
- // dayDiff = today.getDate() + (new Date(today.getFullYear(), today.getMonth(), 0)).getDate() - birth.getDate();
46
+ dayDiff += lastMonth . getDate ( ) ;
36
47
}
37
-
38
- age . years = yearDiff ;
39
- age . months = monthDiff ;
40
- age . days = dayDiff ;
41
-
42
- return age ;
48
+ // dayDiff = today.getDate() + (new Date(today.getFullYear(), today.getMonth(), 0)).getDate() - birth.getDate();
43
49
}
44
50
51
+ age . years = yearDiff ;
52
+ age . months = monthDiff ;
53
+ age . days = dayDiff ;
54
+ // console.log(age.days)
55
+
56
+ return age ;
57
+ }
45
58
46
- btnEl . addEventListener ( "click" , calculateAge ) ;
59
+ btnEl . addEventListener ( "click" , calculateAge ) ;
0 commit comments