|
1 |
| -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. |
| 1 | +/*************************************************************************************** |
| 2 | +* * |
| 3 | +* CODERBYTE BEGINNER CHALLENGE * |
| 4 | +* * |
| 5 | +* Counting Minutes I * |
| 6 | +* Using the JavaScript language, have the function CountingMinutesI(str) take the * |
| 7 | +* str parameter being passed which will be two times (each properly formatted with * |
| 8 | +* a colon and am or pm) separated by a hyphen and return the total number of minutes * |
| 9 | +* between the two times. The time will be in a 12 hour clock format. For example: * |
| 10 | +* if str is 9:00am-10:00am then the output should be 60. If str is 1:00pm-11:00am * |
| 11 | +* the output should be 1320. * |
| 12 | +* * |
| 13 | +* SOLUTION * |
| 14 | +* Sometimes it pays to spend more time mapping out all the possibilites you might * |
| 15 | +* face in solving a problem before you start programming. This challenge will have * |
| 16 | +* 4 possible scenarios. They are: * |
| 17 | +* a) both have same ampm values and first time is after second time * |
| 18 | +* b) both have same ampm values and the first time is before second time * |
| 19 | +* c) have different ampm times and time1 is am * |
| 20 | +* d) have different ampm times and time2 is pm * |
| 21 | +* * |
| 22 | +* I am going to use an object to represent both times simply because I like to use * |
| 23 | +* words like hours, mins, and ampm instead of referring to an array index. To parse * |
| 24 | +* the str into the two time objects I created a separate function. Now that I have * |
| 25 | +* my 2 time object I just need to create a series of IF statements to correspond to * |
| 26 | +* the 4 scenarios listed above to calculate the timeDiff. * |
| 27 | +* * |
| 28 | +* Steps for solution * |
| 29 | +* 1) Use RegExp pattern to search string for pattern a...b * |
| 30 | +* 2) If found return true * |
| 31 | +* 3) Else return false * |
| 32 | +* * |
| 33 | +***************************************************************************************/ |
2 | 34 | function CountingMinutesI(str) {
|
| 35 | + var time1Obj = {}, time2Obj = {}, timeDiff; |
| 36 | + |
| 37 | + time1Obj = setTimeObject(str, 0); |
| 38 | + time2Obj = setTimeObject(str, 1); |
| 39 | + |
| 40 | + if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot > time2Obj.tot) { |
| 41 | + timeDiff = (((12 - time1Obj.hours + 12) * 60) - (time1Obj.mins)) + ((time2Obj.hours * 60) + time2Obj.mins); |
| 42 | + } |
| 43 | + else if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot < time2Obj.tot) { |
| 44 | + timeDiff = ((time2Obj.hours * 60) + time2Obj.mins) - ((time1Obj.hours * 60) + time1Obj.mins); |
| 45 | + } |
| 46 | + else if (time1Obj.ampm !== time2Obj.ampm && time1Obj.ampm === "am") { |
| 47 | + timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); |
| 48 | + } |
| 49 | + else { |
| 50 | + timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); |
| 51 | + } |
3 | 52 |
|
4 |
| - // code goes here |
5 |
| - return 690; |
6 |
| - |
| 53 | + return timeDiff; |
| 54 | +} |
| 55 | + |
| 56 | +function setTimeObject(str, num) { |
| 57 | + var arr = str.split("-"); |
| 58 | + var tObject = {}; |
| 59 | + |
| 60 | + tObject.hours = Number(arr[num].slice(0,arr[num].length-2).split(":")[0]); |
| 61 | + tObject.mins = Number(arr[num].slice(0,arr[num].length-2).split(":")[1]); |
| 62 | + tObject.ampm = arr[num].slice(-2); |
| 63 | + tObject.tot = tObject.hours * 100 + tObject.mins; |
| 64 | + |
| 65 | + return tObject; |
7 | 66 | }
|
8 |
| - |
9 |
| -1. When the input was "1:00pm-11:00am" your output was incorrect. |
10 |
| -2. When the input was "2:03pm-1:39pm" your output was incorrect. |
11 |
| -3. When the input was "1:23am-1:08am" your output was incorrect. |
12 |
| -4. When the input was "2:08pm-2:00am" your output was incorrect. |
13 |
| -5. When the input was "2:00pm-3:00pm" your output was incorrect. |
14 |
| -6. When the input was "11:00am-2:10pm" your output was incorrect. |
15 |
| -7. When the input was "12:31pm-12:34pm" your output was incorrect. |
16 |
| -8. When the input was "3:00pm-4:45am" your output was incorrect. |
17 |
| -9. When the input was "5:00pm-5:11pm" your output was incorrect. |
|
0 commit comments