Hong-Phuc Bui
2026-10-29 c2b4ed4b098ada9aa699f903bcd2c1f7c7acb7a9
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
/*
* 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);
    }
})