Last active
February 20, 2024 00:29
-
-
Save dohooo/27d4e2f1fe726fc156f7cb97074d377f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calcHours(ranges) { | |
const timeRanges = ranges.split(',').map(range => range.trim().split('-')); | |
let totalHours = 0; | |
timeRanges.forEach(range => { | |
const [start, end] = range.map(time => { | |
const [hour, minute] = time.split(':').map(Number); | |
return new Date(2000, 0, 1, hour, minute); | |
}); | |
let diff = (end - start) / (1000 * 60 * 60); | |
if (diff < 0) { | |
diff += 24; | |
} | |
totalHours += diff; | |
}); | |
return totalHours; | |
} | |
if (process.argv.length > 2) { | |
const input = process.argv[2]; | |
const hours = calcHours(input); | |
console.log(hours); | |
} else { | |
console.log("Please provide the time ranges as arguments, separated by commas."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment