<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dog Age Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator-container">
<h1>Dog Age Calculator</h1>
<p>Enter your dog's age in human years:</p>
<input type="number" id="humanYears" placeholder="Enter human years" min="0">
<button onclick="calculateDogAge()">Calculate</button>
<p class="result" id="result"></p>
</div>
<script src="script.js"></script>
</body>
</html>
/* styles.css */
/* Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #ffafbd, #ffc3a0);
color: #333;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.calculator-container {
background: #fff;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
text-align: center;
width: 300px;
}
.calculator-container h1 {
font-size: 24px;
margin-bottom: 20px;
color: #ff6f61;
}
.calculator-container input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
.calculator-container button {
background: #ff6f61;
color: #fff;
border: none;
padding: 10px 15px;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background 0.3s;
}
.calculator-container button:hover {
background: #e55a4e;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
font-weight: bold;
}
// script.js
function calculateDogAge() {
const humanYears = document.getElementById('humanYears').value;
let dogYears;
if (humanYears === "" || isNaN(humanYears) || humanYears <= 0) {
dogYears = "Please enter a valid positive number!";
} else if (humanYears <= 2) {
dogYears = humanYears * 10.5;
} else {
dogYears = (2 * 10.5) + ((humanYears - 2) * 4);
}
document.getElementById('result').innerText =
typeof dogYears === "number"
? `Your dog's age in dog years is: ${dogYears.toFixed(1)}`
: dogYears;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.