How to calculate time between two different times?

yeah that’s what i suggested earlier but the mods are so vague in what their saying, im left with the impression that what im doing is wrong so i go many other countless routes for the past 9 hours. anyways, now converting strings to number format isn’t the problem. now i’m just so confused with the algorithm that i could only pass 1 test case out of 202. my code :

function solve(arr){

let msChecker = 10000000000000000;
for ( let i = 0; i < arr.length; i++) {   
  if (i % 2 === 0) { 
    console.log(arr[i])
let end = new Date("August 21, 2019 " + arr[i]);
let start = new Date("August 21, 2019 " + arr[i+1]);
let ms = Math.floor(end - start);
console.log(ms)
if ( ms < msChecker) { 
  msChecker = ms
}
  }
}
 let minutes = Math.floor(msChecker/1000/60)
let hours =  Math.floor(msChecker/1000/60/60)
console.log(hours)
console.log(minutes)
minutes = minutes - (hours*60)
console.log(minutes)
minutes-=1;
return hours.toString().concat(":",minutes.toString())
}

solve(["23:00","04:22","18:05","06:24"])

// ,"11:40")


// In this Kata, you will be given a series of times at which an alarm goes off. Your task will be to determine the maximum time interval between alarms. Each alarm starts ringing at the beginning of the corresponding minute and rings for exactly one minute. The times in the array are not in chronological order. Ignore duplicate times, if any.

// let sorted = arr.sort((a,b)=> a>b?1:-1)
// console.log(sorted)

// let msCheck = 0;
// for ( let i = 0; i < sorted.length; i++) { 
// let start = new Date("August 21, 2019 " + sorted[i]);

// let j = i+1
// while (j < sorted.length) { 
// let end = new Date("August 21, 2019 " + sorted[j])
// let ms = Math.floor(end - start);
// console.log(ms)
// if (ms > msCheck ) { 
//   msCheck = ms
// }
// j++
// }

// }

// let startt = new Date("August 21, 2019 04:22:00");
// let endd = new Date("August 21, 2019 23:00:00")
// console.log(endd-startt)

don’t take the entirety of my code literally. i did many tweaks to the point im so desperate to even pass 1 test case. if not i wouldn’t even be able to pass a single one.

I took a look at the Kata, and can guide you in the way that I would do it. It seems that you are trying to incorporate Date(), which you wouldn’t need to do.

So first, you know that the array can contain only one number. You can do a simple if statement for that to clear that case, and the great part about this, is that you don’t need to do any calculations, because it it’s only one number, the max interval will always be 23:59. So all you would need to do is just return “23:59”.

For cases that it’s more than one number, you are on the right track with the sort, as you would need to sort them from highest to lowest, of course after converting them into numbers. After you have those sorted numbers, you would need to subtract the two adjacent numbers, while doing so you need to take into account the 1 minute interval that the kata says that the alarm will go off on.

Given that, you would subtract .01 from the result that you get. But theres another catch, if the numbers after the decimal is over 60, that obviously wouldn’t’ be correct, as there’s only 60 minutes in an hour, so to account for that as well, you can do an if statement to check if those preceding numbers after the decimal point is over 60, and if so subtract 40 from it.

After all of that is said and done, you would just return the highest number in that new calculated array, of course converting the dot back to a semicolon. So now it’s just up to you to write that code out with the above logic.

someone was trying to hint that if you separate the time in hours and minutes, instead of separate the string in all its characters, it is much easier to work with it

people are willing to help
if you don’t understand an hint say so
say “I don’t understand what you are hinting”
people try to be the vaguest possible
while giving something useful so that you do the most legwork, and if you need something more you need to say so

I think the biggest rabbit hole of this kata is that clock times are modular by nature. After 24:00 you start again at 00:01 and so on. Simply put, it cycles.

So you need to account for those cases in which there’s a difference between for example, 23 hours and 04 hours, you have to see how many hours until 24 and then add it to the time elapsed on B from 0 to that unless the hour is greater than A and less than or equal to 24.

Examples:

24 -> 24 = 0 // May even be 24 depending on how you view
// things; this is kind of an especial case
23 -> 24 = 1
10 -> 20 = 10
10 -> 9 = 14 + 9 = 23

There’s a pattern here that you need to notice in order to solve it but you also need to take in consideration the minutes, so you need to multiply the hours by 60 and then add the minutes. 24 hours are equivalent to 1440 minutes; just for reference.

Functions you may need:

  • parseInt
  • String.prototype.split
  • Math.abs

After looking at the kata again, I really don’t understand how it’s coming to the maximum time interval. Logically just looking at the time that the alarm goes off, the first 2 test cases makes sense, but then you look at the third one, and it shows that 9 hours and 10 minutes is the longest interval that the alarm did not go off…

Which makes no sense to me, unless I’m looking at it wrong, as if you look in sequential with the time stamp of the time in the array, the longest time frame without it going off is 8 hours and 26 minutes. So I have no idea, if the wording is bad on the test case, or I’m just dumb lol.

Solved it; there’s 2 caveats:

You have to take in consideration the minute it takes to go off, so you add 1.
Also, the times are not sorted by default, you have to sort it yourself in ascending order
You have to add the first item of the list at the end of the list because of the following edge case: the interval between the last time and the first time of the list.

Spoilers of my solution with RamdaJS:

Welp i did what you said and I passed 0 tests out of 202. code :

function solve(arr){ 
  for (let i = 0; i < arr.length; i++) { 
   arr[i] = arr[i].replace(/:/g,".");
  }
  console.log(arr)
let array = []

for ( let i = 0; i < arr.length; i++) { 
  if (i % 2 === 0) { 
    array.push(Number(arr[i]) - Number(arr[i+1])-0.1)
  }
}

console.log(array)
let endArr = [];
for (let num of array) { 
  let integer = Math.floor(num)
  let float = (num%1).toFixed(2)
  
  if (float > 0.60) { 
    float-=0.40;
  }
  let newNum = integer + float;
  endArr.push(newNum)
}

return endArr.sort((a,b)=> b-a)[0].toString()
}

solve(["23:00","04:22","18:05","06:24"])

your code don’t pass on codewars?
edit: also i dont understand ur code at all…

anybody else interested in helping to solve the problem, feel free to lay down your tips and tricks too. i been on the fence of this problem for over 15 hours

1 Like

what are you trying to do?

I see these steps here:

  • convert everything to minutes (split the time, and then hours*60+mins)
  • sort ascending
  • calculate intervals (considering that last item of the array is to be checked with first item of the next day, so adding 24h to it)
  • Math.max() and convert back to time

Of course it doesn’t pass, the library Ramda does not exist in Codewards.

You have to come up with the code to do all those things yourself:

1- How to create intervals:

// go from this
[1,2,3,4]
// to this
[[1,2],[2,3],[3,4]]
// and then to this
[[1,2],[2,3],[3,4],[4,1]]

2- How to convert a string like "HH:MM" to a tuple of [23, 55] for example. And then to the equivalent in total minutes.

3- How to convert an amount of minutes back to the string "HH:MM".

4- How to get the maximum value of of an array, I’ll give you this wisdom for free:

function maximum(list) {
  return Math.max.apply(null, list);
}

5- How to sort an array in ascending order

6- How to get rid of duplicated elements


Now, the core function that ultimately makes the whole challenge take form is:

function diff([minutesA, minutesB]) {
  return minutesB <= minutesA
    ? (1440 - minutesA) + minutesB - 1
    : minutesB - minutesA - 1
}

Remember that 1440 is the equivalent of 24 hours in minutes. This function computes how many minutes apart are A from B. But for this, you have to make sure that the number of minutes from 0:00 up to A or B meet the condition minutesB <= minutesA because remember than subtracting a lower value minus a greater value will yield a negative number and that after 24 hours the number starts from 1 again. The -1 is because the challenge says the alarm goes off for a full minute.

How do you split the time? I been told my way of using split is incorrect.

you were splitting each character, but you can split the time between hours and minutes so you get an array like ["hh", "mm"] and it easier to make any calculation
you just need to use the right argument for split

This is how you do it:
convert both times into minutes. 18hr60min/hr + 5 - 6hr60min/hr + 24 = 701min

then convert 701min into hours and mins:
701 % 60 = 11 hrs
701 // 60 = 41 syntax assumed here is for python.

Time will 11:41 hrs