1

I'm trying to create a function which would add together all the values from an array of times.

const times = ["00:00:50", "00:03:20", "00:00:50"]

What I've got so far

  function sumTime(t1, t2, array = []) {
    const times = [3600, 60, 1],
      sum = [t1, t2, ...array]
        .map((s) => s.reduce((s, v, i) => s + times[i] * v, 0))
        .reduce((a, b) => a + b, 0);
    return times
      .map((t) => [Math.floor(sum / t), (sum %= t)][0])
      .map((v) => v.toString().padStart(2, 0));
  }
sumTime(times)

But I'm getting a error Cannot read property 'reduce' of undefined. Not sure where I'm going wrong?

David R
  • 173
  • 8
  • What result are you trying to achieve? Also you are calling function sumTime(times) and passing one parameter. So your t1 is times array. T2 is undefined You can easily console.log that, its debugging 1on1. Console.log every step – ikiK Mar 09 '21 at 12:28
  • OK. I just want to add "00:00:50", "00:03:20", "00:00:50" together which would result in 00:05:00 – David R Mar 09 '21 at 12:55

4 Answers4

1

This would be my approach of doing it: Converting the times in hh:mm:ss format to numbers will make it easier to add them together. At the end you can just convert it back to a Date and call toISOString(). The substr(11, 8) will give you the hh:mm:ss back.

const times = ["00:00:50", "00:03:20", "00:00:50"];
    
function sumTime(times) {
  let sumSeconds = 0;
    
  times.forEach(time => {
    let a = time.split(":");
    let seconds = +a[0] * 60 * 60 + +a[1] * 60 + +a[2];
    sumSeconds += seconds;
  });
    
  return new Date(sumSeconds * 1000).toISOString().substr(11, 8);
}
    
console.log(sumTime(times));    //00:05:00
Behemoth
  • 5,389
  • 4
  • 16
  • 40
0

t2 is undefined in this scenario because you're calling the function with only one parameter.

I'm asking myself why you even need this parameter? One can simply put all the times to be summed up in one array and pass it to this function. Just like this:

function sumTime(t1, array = []) {
const times = [3600, 60, 1],
  sum = [t1, ...array]
    .map((s) => s.reduce((s, v, i) => s + times[i] * v, 0))
    .reduce((a, b) => a + b, 0);
return times
  .map((t) => [Math.floor(sum / t), (sum %= t)][0])
  .map((v) => v.toString().padStart(2, 0));

}

0

Useful links:

Convert HH:MM:SS string to seconds only in javascript

add up numbers in array

Convert seconds to HH-MM-SS with JavaScript?

const times = ["00:00:50", "00:03:20", "00:00:50"]

function sumTime(times) {
let result=[]
  times.forEach(time => {
    let t = time.split(":");
    let seconds = (+t[0]) * 60 * 60 + (+t[1]) * 60 + (+t[2])
    result.push(seconds)
  })
    let res = result.reduce((r,c) => r + parseFloat(c), 0)
    let r =  new Date(res * 1000).toISOString().substr(11, 8)
    console.log(r)
}

sumTime(times)
ikiK
  • 6,328
  • 4
  • 20
  • 40
0

function summarizeHHMMSS(hhmmssList) {
  return new Date(hhmmssList.reduce((seconds, hhmmss) => {

    const [ hh, mm, ss ] = hhmmss.split(':');
    return (
      seconds +

      (ss * 1) +
      (mm * 60) +
      (hh * 3600)
    );

  }, 0) * 1000).toISOString().slice(11,19);
}

console.log(
  summarizeHHMMSS(["00:00:50", "00:03:20", "00:00:50"])
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37