Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project 17 completed #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ This repository contains a collection of frontend projects.Each project is built
<td>Pricing card</td>
<td><a href="./project-16_pricing_component"/>Click Here</a></td>
</tr>
<tr>
<td>17</td>
<td>Age Calculator</td>
<td><a href="./project-17_Age_calculator"/>Click Here</a></td>
</tr>
</table>


Expand Down
68 changes: 68 additions & 0 deletions project-17_Age_calculator/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
padding: 20px;
font-family: "montserrat", sans-serif;
background-color: #f7f7f7;
}
.container{
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
padding: 20px;
max-width: 600px;
margin:50px auto 0;
border-radius: 5px;
}
h1{
font-size: 36px;
text-align: center;
margin-top: 0;
margin-bottom: 20px;
}
.form{
display: flex;
flex-direction: column;
align-items: center;
}
label{
font-weight: bold;
margin-bottom: 10px;
}
input{
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px;
width: 100%;
max-width: 300px;
}
button{
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
margin-top: 10px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover{
background-color: #0062cc;
}
#result{
margin-top: 20px;
font-size: 24px;
font-weight: bold;
}
@media only screen
and (min-device-width: 320px)
and (max-device-width: 480px) {
body{
background-color: bisque;
}
.container{
max-width: 300px;
}
}
21 changes: 21 additions & 0 deletions project-17_Age_calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Age Calculator</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<div class="container">
<h1>Age Calculator</h1>
<div class="form">
<label for="birthday">Enter your date of birth</label>
<input type="date" id="birthday" name="birthday">
<button id="btn">Calculate Age</button>
<p id="result">your age is 21 years old</p>
</div>
</div>
<script src="./script/script.js"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions project-17_Age_calculator/script/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const btnEl = document.getElementById('btn');
const birthdayEl = document.getElementById('birthday');
const resultEl = document.getElementById('result');

function calculateAge() {
const birthdayValue = birthdayEl.value;
if (birthdayValue === " ") {
alert("please enter your birthday");
} else {
const age = getAge(birthdayValue);
resultEl.innerText = `your age is ${age} ${age > 1 ? 'years' : 'years'} old`;
}
}

function getAge(birthdayValue) {
const currentDate = new Date();
const birthdayDate = new Date(birthdayValue);
let age = currentDate.getFullYear() - birthdayDate.getFullYear();
const month = currentDate.getMonth() - birthdayDate.getMonth();

if (month < 0 || (month == 0 && currentDate.getDate()
< birthdayDate.getDate()))
{
age--;
}
return age;
}
btnEl.addEventListener('click', calculateAge);