Skip to content

Instantly share code, notes, and snippets.

@mojalil
Created July 5, 2023 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojalil/ea8f9147e119db5d3505de35c7698038 to your computer and use it in GitHub Desktop.
Save mojalil/ea8f9147e119db5d3505de35c7698038 to your computer and use it in GitHub Desktop.
nounsrc split time converter
function convertToTimedelta(splitTime) {
const [minutes, seconds, milliseconds] = splitTime.split(/[:.]/).map(Number);
return minutes * 60 + seconds + milliseconds / 1000;
}
function calculateCumulativeSplitTimes(splitTimes) {
const cumulativeTimes = [];
let cumulativeTime = 0;
for (const splitTime of splitTimes) {
cumulativeTime += splitTime;
cumulativeTimes.push(cumulativeTime);
}
return cumulativeTimes;
}
function printCumulativeSplitTimes(names, cumulativeTimes) {
const sortedData = names.map((name, index) => ({ name, time: cumulativeTimes[index] }))
.sort((a, b) => a.time - b.time);
console.log("Name\t\tCumulative Time");
console.log("-----------------------------");
for (const { name, time } of sortedData) {
console.log(`${name}\t${time/60}`);
}
}
// Split times as deltas (reverse the order of the array)
const splitTimes = [
"00:01.78",
"03:09.26",
"00:00.59",
"01:22.88",
"00:02.00",
"00:27.66",
"06:23.23"
].reverse();
// Names of the runners (reverse the order of the array)
const names = [
"Yen",
"Skipper",
"Sim",
"Amanda",
"Maggie",
"Yizhen",
"Brian"
].reverse();
// Convert split times to timedelta objects
const convertedSplitTimes = splitTimes.map(convertToTimedelta);
// Calculate cumulative split times
const cumulativeTimes = calculateCumulativeSplitTimes(convertedSplitTimes);
// Print the results
printCumulativeSplitTimes(names, cumulativeTimes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment