View difference between Paste ID: B2fdkchj and KtFsJ2TK
SHOW: | | - or go back to the newest paste.
1
function solve(examHour, examMinutes, arriveHour, arriveMinutes){
2
    examHour = Number(examHour);
3
    examMinutes = Number(examMinutes);
4
    arriveHour = Number(arriveHour);
5
    arriveMinutes = Number(arriveMinutes);  
6
7
    let examInMinutes = examHour * 60 + examMinutes;
8
    let arriveInMinutes = arriveHour * 60 + arriveMinutes;
9
    let late = arriveInMinutes - examInMinutes;
10
    let early = examInMinutes - arriveInMinutes;
11
    let diff = Math.abs(late);
12-
    let hours = parseInt(diff / 60);
12+
    let hours = parseInt(diff / 60);  // взима целочислената част от делението
13
    let minutes = diff % 60;
14
    
15
    const zeroPad = (num, places) => String(num).padStart(places, '0')
16
	// https://stackoverflow.com/questions/2998784/how-to-output-numbers-with-leading-zeros-in-javascript
17
18
    if (late > 0){
19
        console.log(`Late`)
20
        if (late <= 59){
21
            console.log(`${late} minutes after the start`)
22
        }
23
        else{
24
            console.log(`${hours}:${zeroPad(minutes, 2)} hours after the start`)
25
        }
26
    }
27
    else if (early >= 0 && early <= 30){
28
        console.log(`On time`)
29
        if (early != 0){
30
            console.log(`${early} minutes before the start`)
31
        }
32
    }
33
    else if (early > 30){
34
        console.log(`Early`)
35
        if (early <= 59){
36
            console.log(`${early} minutes before the start`)
37
        }
38
        else{
39
            console.log(`${hours}:${zeroPad(minutes, 2)} hours before the start`)
40
        }
41
    }
42
}