-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.html
More file actions
247 lines (213 loc) · 7.87 KB
/
calculator.html
File metadata and controls
247 lines (213 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gen Z Age Calculator</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f2f5;
}
.container {
background: #fff;
width: 400px;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
margin-bottom: 1.5rem;
color: #333;
}
/* The Result Box Styling */
.result-box {
display: none; /* Hidden by default */
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
font-size: 0.9rem;
line-height: 1.4;
font-weight: 500;
border-left: 5px solid transparent;
}
/* Variants for the result box */
.result-success {
background-color: #d1e7dd;
color: #0f5132;
border-left-color: #0f5132;
}
.result-warning {
background-color: #fff3cd;
color: #664d03;
border-left-color: #664d03;
}
.result-danger {
background-color: #f8d7da;
color: #842029;
border-left-color: #842029;
}
.form-group {
margin-bottom: 1.2rem;
}
label {
display: block;
margin-bottom: 0.5rem;
font-size: 0.9rem;
color: #555;
}
input[type="text"], select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 6px;
outline: none;
font-size: 1rem;
color: #333;
background-color: #fff;
}
input[type="text"]:focus, select:focus {
border-color: #dc3545;
}
.date-selectors {
display: flex;
gap: 10px;
}
.date-selectors .select-group {
flex: 1;
}
button {
width: 100%;
padding: 12px;
background-color: #dc3545; /* Reddish pink from video */
color: #fff;
border: none;
border-radius: 6px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background 0.3s;
margin-top: 10px;
}
button:hover {
background-color: #c82333;
}
/* Small responsive adjustment */
@media (max-width: 420px) {
.container {
width: 90%;
}
}
</style>
</head>
<body>
<div class="container">
<h2>Age Calculator</h2>
<div id="result" class="result-box"></div>
<div class="form-group">
<label for="fullName">Full Name</label>
<input type="text" id="fullName" placeholder="Sandeep Kumar">
</div>
<div class="form-group">
<label for="day">Date</label>
<select id="day">
<option value="" disabled selected>Select Day</option>
</select>
</div>
<div class="form-group">
<label for="month">Month</label>
<select id="month">
<option value="" disabled selected>Select Month</option>
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
</div>
<div class="form-group">
<label for="year">Year</label>
<select id="year">
<option value="" disabled selected>Select Year</option>
</select>
</div>
<button onclick="calculateAge()">Calculate Age</button>
</div>
<script>
// 1. Populate Days (1-31)
const daySelect = document.getElementById('day');
for (let i = 1; i <= 31; i++) {
let option = document.createElement('option');
option.value = i;
option.text = i;
daySelect.appendChild(option);
}
// 2. Populate Years (1900 - Current Year)
const yearSelect = document.getElementById('year');
const currentYear = new Date().getFullYear();
for (let i = currentYear; i >= 1900; i--) {
let option = document.createElement('option');
option.value = i;
option.text = i;
yearSelect.appendChild(option);
}
// 3. Calculation Logic
function calculateAge() {
const nameInput = document.getElementById('fullName').value;
const day = parseInt(document.getElementById('day').value);
const month = parseInt(document.getElementById('month').value);
const year = parseInt(document.getElementById('year').value);
const resultBox = document.getElementById('result');
// Basic Validation
if (!nameInput || isNaN(day) || isNaN(month) || isNaN(year)) {
alert("Please fill in all fields correctly!");
return;
}
const name = nameInput.trim();
const today = new Date();
const birthDate = new Date(year, month, day);
let age = today.getFullYear() - birthDate.getFullYear();
const m = today.getMonth() - birthDate.getMonth();
// Adjust age if birthday hasn't happened yet this year
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
// Reset classes
resultBox.className = 'result-box';
resultBox.style.display = 'block';
// Logic for the specific messages seen in the video
if (year >= 2001) { // (age < 30) {
// Young (Green Box)
resultBox.classList.add('result-success');
resultBox.innerHTML = `👋 <strong>${name}</strong>, you are ${age} years old.<br>Chill bro, life just started 😎`;
} else if (year < 2001 && year > 1997) { // (age >= 30 && age < 50) {
// Middle Age (Yellow/Warning Box)
resultBox.classList.add('result-warning');
resultBox.innerHTML = `😐 <strong>${name}</strong>, you are ${age} years old.<br>You are officially an old now 💀`;
} else {
// Old (Red/Danger Box)
resultBox.classList.add('result-danger');
resultBox.innerHTML = `😵 <strong>${name}</strong>, you are ${age} years old.<br>You are toooooo much old. Please stop wasting our server resources, stop counting your age. 🙏`;
}
}
</script>
</body>
</html>