Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
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
import {parseRatio, parseDiagonal, calculateTvSize, formatTvSize} from "./tv-size.js";
 
document.addEventListener("DOMContentLoaded", () => {
    updateTvParameter();
    document.getElementById("aspect-ratio")
        .addEventListener("change", updateTvParameter);
    document.getElementById("diagonal")
        .addEventListener("input", updateTvParameter);
 
});
 
 
function updateTvParameter() {
    try {
        const ratioInput = document.getElementById("aspect-ratio");
        const ratio = parseRatio(ratioInput.value);
        console.log(ratio);
        const diagonalInput = document.getElementById("diagonal");
        const diagonal = parseDiagonal(diagonalInput.value);
        const size = calculateTvSize(ratio, diagonal);
        showTvSize(...size);
        turnOffErrorDisplay();
    }catch (e) {
        showInputErrorMessage(e);
    }
}
 
function showTvSize(width, height) {
    const [widthText, heightText] = formatTvSize(width, height);
    document.getElementById("tv-height").innerText = heightText;
    document.getElementById("tv-width").innerText = widthText;
}
 
 
function showInputErrorMessage(e) {
    const errorMsg = e.message;
    const errorOutputElement = document.getElementById("error-msg");
    errorOutputElement.innerText = errorMsg;
    errorOutputElement.classList.remove("no-error");
    errorOutputElement.classList.add("error");
}
 
function turnOffErrorDisplay() {
    const errorOutputElement = document.getElementById("error-msg");
    errorOutputElement.classList.remove("error");
    errorOutputElement.classList.add("no-error");
}
 
 
const dayCount = (month, year) => {
    if (month !== 2) {
        return ( ((month-1)%7)%2 ) ? 30 : 31;
    }
    var isLeap = !(year % 4);
    if(isLeap) {isLeap = !!(year%100) }
    if(!isLeap) {isLeap = !(year%400) }
    return isLeap ? 29:28;
}