Minutes Between Events
Programming Basics · JavaScript
TASK
Problem
Given two times `h1 m1 h2 m2`. The second event may be next day. Print non-negative minutes from first to second.
EXAMPLE
Example
23 50 0 10
20
LIMITS
Constraints
0≤h≤23; 0≤m≤59
LEARN
Theory for this problem
+
### Minutes Between Events
Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.
NEED HELP?
Hints
+
['Decide which part comes from division and which part comes from the remainder.', 'Mentally substitute at least one value for each branch and verify that exactly the intended branch runs.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [h1, m1, h2, m2] = input.trim().split(/\s+/).filter(Boolean).map(Number);
let a = (((h1) * (60)) + m1);
let b = (((h2) * (60)) + m2);
if (b < a) {
b += 1440;
}
console.log(b - a);