0

I am working on a course registration system.I need to check for time conflicts.

Already registered courses object:

{"00001":{"days":"Monday-Tuesday","hours":"11:40-12:30*13:40-15:30"}}

this means that 00001 course is in monday 11:40-12:30 in tuesday 13:40-15:30

Courses to register object:

{"00003":{"days":"Friday","hours":"9:40-10:40"}}

I have managed to check is student already registered to course with this code:

Object.keys(registeredcoursesobject).forEach(function(key){
    if( Object.keys(coursestoregisterobject).includes(key)) {
      alert("You have already registered to "+key+" crn number course");

      //return;
    }
  });

A course can be at most 2 days in a week and in 1 different time intervals(what if 2 time intervals??) which means that there will be only one "-" in days property and only one "*" in hours property. I am new to programming and working on this for days any ideas ?

newjsstu
  • 11
  • 3
  • can you be more specific, what objects you need to compare and on what criteria you want to compare them? – andnik Feb 24 '18 at 13:07
  • As I understood, you have array of objects like: `{"00001":{"days":"Monday-Tuesday","hours":"11:40-12:30*13:40-15:30"}}` and you have incoming object `{"00003":{"days":"Friday","hours":"9:40-10:40"}}`, where you need to check if your array of courses has course with same date and time? – andnik Feb 24 '18 at 13:09
  • for example first i need to check for days.If one of the days are matching in both objects i need to check for hours.If one objects has hours : 12.40-15.30 and other 1.40-2.40 because there is a conflict give error – newjsstu Feb 24 '18 at 13:12

1 Answers1

0

I hope this answer is still relevant for you. Here is what I have:

var registeredcoursesobject = {"00001":{"days":"Monday-Thursday","hours":"11:40-12:30*16:30-18:30"}}
var coursestoregisterobject = {"00002":{"days":"Monday-Friday","hours":"10:40-15:30*16:40-18:00"}}

var getTicks = function(timeStr) {
    return new Date('1970-01-01T' + timeStr + ':00Z').getTime();
}

Object.keys(registeredcoursesobject).forEach(function(rKey){
    if( Object.keys(coursestoregisterobject).includes(rKey)) {
        alert("You have already registered to "+rKey+" crn number course");
        return false;
    };
    Object.keys(coursestoregisterobject).forEach(function(cKey){
        var regDays = registeredcoursesobject[rKey].days.split('-');
        var regHours = registeredcoursesobject[rKey].hours.split('*');

        var courseDays = coursestoregisterobject[cKey].days.split('-');
        var courseHours = coursestoregisterobject[cKey].hours.split('*');

        regDays.forEach(function(rDay, i) {
            var rHourRange = regHours[i];

            // I assume you need to check there is same date/time pain in registeredcoursesobject and coursestoregisterobject
            courseDays.forEach(function(cDay, j) {
                if (rDay == cDay) {
                    var cHourRange = courseHours[j];
                    // now, do you need to compare hours be equal exactly or do you need to check time overlap?
                    // assume you just need to ckeck hour ranges are equal, then:
                    if (rHourRange == cHourRange){
                        // means equal
                        alert("You have already registered to "+cKey+" crn number course on day "+cDay+" at "+cHourRange+" hours.");
                        return true;
                    }

                    // if you need to check range overlap
                    var rTime = rHourRange.split('-');
                    rTimeRange = [getTicks(rTime[0]), getTicks(rTime[1])];
                    rStartT = Math.min.apply(null, rTimeRange), rEndT = Math.max.apply(null, rTimeRange);
                    var cTime = cHourRange.split('-');
                    cTimeRange = [getTicks(cTime[0]), getTicks(cTime[1])]
                    cStartT = Math.min.apply(null, cTimeRange), cEndT = Math.max.apply(null, cTimeRange);

                    // now your rangeTime is a pair of int values, that represent time range rStartT:rEndT
                    // and your courseTime is a pair of int values cStartT:cEndT
                    // so now you just check the overlap of two integer pais.
                    // according to this: https://stackoverflow.com/questions/3269434/whats-the-most-efficient-way-to-test-two-integer-ranges-for-overlap#answer-3269471
                    if (rStartT < cEndT && cStartT < rEndT) {
                        alert("You have already registered to "+cKey+" crn number course on day "+cDay+" within time range "+cHourRange+" hours overlap with "+rHourRange+" time range.");
                        // means time ranges are overlap at some range. But I don't count the border, like "14:00-15:00" and "15:00-16:00" do not overlap
                        // otherwise replace < with <=
                        return true;
                    }
                }
            })
        });
        return false;
    });
});

I am making some assumptions here about your task.

UPDATE: added time range check. UPDATE: check keys equal first and values swap if start time is for some reason is bigger than end time.

andnik
  • 2,405
  • 2
  • 22
  • 33
  • thanks i will look into it btw need to check for time overlap – newjsstu Feb 25 '18 at 11:05
  • var registeredcoursesobject = {"00001":{"days":"Monday-Thursday","hours":"11:40-12:30*16:30-18:30"}} var coursestoregisterobject = {"00001":{"days":"Thursday","hours":"16:40-15:30"}} this failed.student has already registered course between 16:30-18:30 on thursday trying to register a course on thursday hours 16:40-15:30 must throw alert – newjsstu Feb 25 '18 at 15:46
  • "16:40-15:30" don't you think this is wrong time range? first time should be bigger than second. – andnik Feb 25 '18 at 15:47
  • otherwise I suggest you add values cStartT and cEndT swapping if cEndT < cStartT – andnik Feb 25 '18 at 15:48
  • var registeredcoursesobject = {"00001":{"days":"Monday-Thursday","hours":"11:40-12:30*16:30-18:30"}} var coursestoregisterobject = {"00002":{"days":"Monday-Friday","hours":"10:40-15:30*16:40-18:00"}} – newjsstu Feb 25 '18 at 15:54
  • yeah, 11:40-12:30 and 10:40-15:30 overlap right? But your id 00001 and 00002 doesn't match. when you change id to be equal, it works. – andnik Feb 25 '18 at 20:14
  • the thing is if ids are same user is already registered to the course so it doenst matter to do time day checks if ids are not same actually we need to do the time-day checks :) i guess if you modify the code like this its done – newjsstu Feb 25 '18 at 23:16
  • updated to check id first, and then other things. I hope for a best answer vote here man:) I also added to swap start and end time if for some reason you have time range like: "16:40-15:30" – andnik Feb 26 '18 at 08:31