Skip to content

See issue cited for master branch #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 70 additions & 52 deletions counting-minutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,80 @@ Have the function CountingMinutes(str) take the str parameter being passed which

function CountingMinutes(str) {

function ClockTime(ampm, hour, minute) {
if (ampm === 'am') {
this.am = true;
this.pm = false;
} else {
this.am = false;
this.pm = true;
}

this.hour = hour;
this.minute = minute;
}
function Clocktime(ampm, hour, minute) {
if (ampm === "am") {
this.am = true;
this.pm = false;
} else {
this.pm = true;
this.am = false;
}
this.minute = minute;
this.hour = hour;

ClockTime.prototype.eventTime = function () {
if (this.am) {
return this.hour * 60 + this.minute;
} else {
return 12*60 + this.hour * 60 + this.minute;
}
};

ClockTime.prototype.timeTillEOD = function () {
return 24 * 60 - this.eventTime();
};
Clocktime.prototype.timeFromBOD = function() { //Clocktime object method (note the exception made for noon & midnight hours)
if (this.am) {
if (this.hour === 12) {
return this.minute;
}
else {return this.hour * 60 + this.minute;
}
}
if (this.pm) {
if (this.hour === 12) {
return this.hour * 60 + this.minute;
}
else {return 12 * 60 + this.hour * 60 + this.minute;
}
}
}

var times = str.split('-');
var time1_ampm = times[0].substring(times[0].length - 2);
var time1_hour = parseInt(times[0].split(':')[0], 10);
var time1_min = parseInt(times[0].split(':')[1], 10);
Clocktime.prototype.timeTillEOD = function() { //Clocktime object method (note the exception made for noon & midnight hours)
if (this.am) {
if (this.hour === 12) {
return 24 * 60 - (this.minute);
}
else {return 24 * 60 - (this.hour * 60 + this.minute);
}
}
if (this.pm) {
if (this.hour === 12) {
return 24 * 60 - (this.hour * 60 + this.minute);
}
else {return 12 * 60 - (this.hour * 60 + this.minute);
}
}
}

var time2_ampm = times[1].substring(times[1].length - 2);
var time2_hour = parseInt(times[1].split(':')[0], 10);
var time2_min = parseInt(times[1].split(':')[1], 10);
var times = str.split("-");
var time1ampm = times[0].substring(times[0].length-2);
var time2ampm = times[1].substring(times[1].length-2);
var time1minute = parseInt(times[0].slice(0, times[0].length-2).split(":")[1], 10);
var time2minute = parseInt(times[1].slice(0, times[1].length-2).split(":")[1], 10);
var time1hour = parseInt(times[0].slice(0, times[0].length-2).split(":")[0], 10); //here, parseInt doesn't change the radix but does convert the string # to a number value
var time2hour = parseInt(times[1].slice(0, times[1].length-2).split(":")[0], 10);
var time1 = new Clocktime(time1ampm, time1hour, time1minute); //object instances
var time2 = new Clocktime(time2ampm, time2hour, time2minute);

var time1 = new ClockTime(time1_ampm, time1_hour, time1_min);
var time2 = new ClockTime(time2_ampm, time2_hour, time2_min);

if (time1.am && time2.pm) {
return time2.eventTime() - time1.eventTime();
} else if (time1.pm && time2.am) {
return time1.timeTillEOD() + time2.eventTime();
} else if (time1.am && time2.am) {
if (time1.eventTime() < time2.eventTime()) {
return time2.eventTime() - time1.eventTime();
} else {
return time1.timeTillEOD() + time2.eventTime();
}
} else if (time1.pm && time2.pm) {
if (time1.eventTime() < time2.eventTime()) {
return time2.eventTime() - time1.eventTime();
} else {
return time1.timeTillEOD() + time2.eventTime();
}
}


if (time1.am && time2.am) {
if (time1.timeFromBOD() < time2.timeFromBOD()) {
return time2.timeFromBOD() - time1.timeFromBOD();
} else {
return time1.timeTillEOD() + time2.timeFromBOD();
}
}
if (time1.am && time2.pm) {
return time2.timeFromBOD() - time1.timeFromBOD();
} else if (time1.pm && time2.am) {
return time1.timeTillEOD() + time2.timeFromBOD();
}
if (time1.pm && time2.pm) {
if (time1.timeFromBOD() < time2.timeFromBOD()) {
return time2.timeFromBOD() - time1.timeFromBOD();
} else {
return time1.timeTillEOD() + time2.timeFromBOD();
}
}
}

// this call is needed to test your function
Expand Down