Skip to content

Instantly share code, notes, and snippets.

@ikyamelia
Created August 12, 2021 04:57
Show Gist options
  • Save ikyamelia/a95afbc0fee6bec8e968039896bce806 to your computer and use it in GitHub Desktop.
Save ikyamelia/a95afbc0fee6bec8e968039896bce806 to your computer and use it in GitHub Desktop.
Tech Tryout Glints - 03
function CountingMinutesI(str) {
var time10bj = {}, time20bj = {}, timeDiff;
time10bj = setTimeObject(str, 0);
time20bj = setTimeObject(str, 1);
if (time10bj.ampm == time20bj.ampm && time10bj.tot > time20bj.tot) {
timeDiff = (((12 - time10bj.hours + 12) * 60) - (time10bj.mins)) + ((time20bj.hours * 60) + time20bj.mins);
}
else if (time10bj.ampm == time20bj.ampm && time10bj.tot < time20bj.tot) {
timeDiff = ((time20bj.hours * 60) + time20bj.mins) - ((time10bj.hours * 60) + time10bj.mins);
}
else if (time10bj.ampm !== time20bj.ampm && time10bj.ampm === "am") {
timeDiff = (((12 - time10bj.hours) * 60) - time10bj.mins) + ((time20bj.hours * 60) + time20bj.mins);
}
else {
timeDiff = (((12 - time10bj.hours) * 60) - time10bj.mins) + ((time20bj.hours * 60) + time20bj.mins);
}
return timeDiff;
}
function setTimeObject(str, num) {
var arr = str.split("-");
var tObject = {};
tObject.hours = Number(arr[num].slice(0,arr[num].length-2).split(":")[0]);
tObject.mins = Number(arr[num].slice(0,arr[num].length-2).split(":")[1]);
tObject.ampm = arr[num].slice(-2);
tObject.tot = tObject.hours * 100 + tObject.mins;
return tObject
}
// keep this function call here
console.log(CountingMinutesI(readline()));

Counting Minutes I

Have the function CountingMinutesI(str) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:00am-10:00am then the output should be 60. If str is 1:00pm-11:00am the output should be 1320.

Examples

Input: "12:30pm-12:00am" Output: 690 Input: "1:23am-1:08am" Output: 1425

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment