Time Difference
Programming Basics · JavaScript
TASK
Problem
Given two times in one day `h1 m1 h2 m2`, with the second not earlier, print the difference in minutes.
EXAMPLE
Example
10 15 12 0
105
LIMITS
Constraints
0≤h≤23; 0≤m≤59; second ≥ first
LEARN
Theory for this problem
+
### Time Difference
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.', 'Match each input value type to the operation performed on it: JavaScript string and numeric behavior differ significantly.']
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 t1 = (((h1) * (60)) + m1);
let t2 = (((h2) * (60)) + m2);
console.log(t2 - t1);