-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathCounting Minutes I
66 lines (60 loc) · 4.1 KB
/
Counting Minutes I
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/***************************************************************************************
* *
* CODERBYTE BEGINNER CHALLENGE *
* *
* Counting Minutes I *
* Using the JavaScript language, 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. *
* *
* SOLUTION *
* Sometimes it pays to spend more time mapping out all the possibilites you might *
* face in solving a problem before you start programming. This challenge will have *
* 4 possible scenarios. They are: *
* a) both have same ampm values and first time is after second time *
* b) both have same ampm values and the first time is before second time *
* c) have different ampm times and time1 is am *
* d) have different ampm times and time2 is pm *
* *
* I am going to use an object to represent both times simply because I like to use *
* words like hours, mins, and ampm instead of referring to an array index. To parse *
* the str into the two time objects I created a separate function. Now that I have *
* my 2 time object I just need to create a series of IF statements to correspond to *
* the 4 scenarios listed above to calculate the timeDiff. *
* *
* Steps for solution *
* 1) Use RegExp pattern to search string for pattern a...b *
* 2) If found return true *
* 3) Else return false *
* *
***************************************************************************************/
function CountingMinutesI(str) {
var time1Obj = {}, time2Obj = {}, timeDiff;
time1Obj = setTimeObject(str, 0);
time2Obj = setTimeObject(str, 1);
if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot > time2Obj.tot) {
timeDiff = (((12 - time1Obj.hours + 12) * 60) - (time1Obj.mins)) + ((time2Obj.hours * 60) + time2Obj.mins);
}
else if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot < time2Obj.tot) {
timeDiff = ((time2Obj.hours * 60) + time2Obj.mins) - ((time1Obj.hours * 60) + time1Obj.mins);
}
else if (time1Obj.ampm !== time2Obj.ampm && time1Obj.ampm === "am") {
timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins);
}
else {
timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.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;
}