Hong-Phuc Bui
2026-10-29 c2b4ed4b098ada9aa699f903bcd2c1f7c7acb7a9
Leapyear
2 files added
67 ■■■■■ changed files
03-0-leapyear/index.html 15 ●●●●● patch | view | raw | blame | history
03-0-leapyear/leap.js 52 ●●●●● patch | view | raw | blame | history
03-0-leapyear/index.html
New file
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Leapyear</title>
</head>
<body>
<input type="text" id="year">
<button type="button" id="ok">Ok</button>
<p id="output"></p>
<p id="error-output"></p>
<script src="leap.js"></script>
</body>
</html>
03-0-leapyear/leap.js
New file
@@ -0,0 +1,52 @@
/*
* Schaltjahr
* durch 4 teilbar
* wenn das jahr durch 100 teilbar dann ist das jahr kein
* schaltjahr aber wenn das Jahr durch 400 teilbar dann das jahr ist
* wiederum ein schaltjahr
* */
const isLeapyear = (year) =>
    ((year % 4) === 0) && (year % 100 !== 0) || (year % 400 === 0);
const validateInputYear = (inputYear) => {
    const year = Number(inputYear);
    if(Number.isNaN(year)) {
        throw new Error("Invalid input");
    }
    if(! Number.isInteger(year)) {
        throw new Error("Invalid input");
    }
    if (year < 1582) {
        throw new Error("Invalid year");
    }
}
const resetOutput = () => {
    const output = document.getElementById("output");
    const errorContainer = document.getElementById("error-output");
    errorContainer.textContent = "";
    output.textContent = "";
}
const showUserError = (error) => {
    const errorContainer = document.getElementById("error-output");
    errorContainer.textContent = e.message
}
document.getElementById("ok").addEventListener("click", ()  => {
    const year = document.getElementById("year").value;
    const output = document.getElementById("output");
    resetOutput();
    try {
        validateInputYear(year);
        if (isLeapyear(year)) {
            output.textContent = "ist ein Schaltjahr";
        }else {
            output.textContent = "ist KEIN Schaltjahr";
        }
    }catch (e) {
        showUserError(e);
    }
})