• + 0 comments

    JavaScript

    function formatDateString(gregorianDate) {
        const date = gregorianDate.split(" ");
        const day = date[0].padStart(2, "0");
        const month = date[1].padStart(2, "0");
        const year = date[2].padStart(4, "0");
        return `${year}-${month}-${day}`;
    }
    
    function calculateFine(dateReturned, dateDue) {
        const returnedDate = new Date(dateReturned);
        const dueDate = new Date(dateDue);
        const distanceInDays = (returnedDate - dueDate) / (1000 * 60 * 60 * 24);
    
        if (distanceInDays <= 0) return 0;
        
        const chargePerDay = 15;
        const chargePerMonth = 500;
        const chargePerYear = 10000;
        
        const daysLate = returnedDate.getDate()  - dueDate.getDate();
        const monthsLate = returnedDate.getMonth() - dueDate.getMonth();
        const yearsLate = returnedDate.getFullYear() - dueDate.getFullYear();
    
        if (yearsLate > 0) {
            return chargePerYear;
        }
        
        if (monthsLate > 0) {
            return monthsLate * chargePerMonth;
        }
        
        if(daysLate > 0) {
            return daysLate * chargePerDay;
        }
    }
    
    function processData(input) {
        var input_stdin_array = input.split("\n");
        const dateReturned = formatDateString(input_stdin_array[0]);
        const dateDue = formatDateString(input_stdin_array[1]);
       
        console.log(calculateFine(dateReturned, dateDue));
    }