Skip to content

Commit 0abc0de

Browse files
committedApr 30, 2023
fix everything
1 parent 817bf45 commit 0abc0de

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed
 

‎projects/age-calculator/index.js

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,51 @@ function calculateAge() {
99
alert("Please enter your birthday");
1010
} else {
1111
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`;
1324
}
1425
}
1526
// 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
1629

1730
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) {
3345
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();
3647
}
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();
4349
}
4450

51+
age.years = yearDiff;
52+
age.months = monthDiff;
53+
age.days = dayDiff;
54+
// console.log(age.days)
55+
56+
return age;
57+
}
4558

46-
btnEl.addEventListener("click", calculateAge);
59+
btnEl.addEventListener("click", calculateAge);

0 commit comments

Comments
 (0)
Please sign in to comment.