首页 > 解决方案 > 计算整体锻炼时间的“持续时间”

问题描述

我有一个包含一些锻炼数据的数组。我正在计算整体锻炼持续时间。

const exercises = [
  {
    title: 'Abdominal Crunch',
    id: '4YRrftQysvE1Kz8XACtrq4',
    rest: ['30', '30', '30', '30', '30', '', '', '', '', ''],
    reps: ['5', '5', '5', '5', '5', '', '', '', '', ''],
    durationBased: false,
    duration: ['', '', ''],
  },
  {
    title: 'Bicep curl',
    id: 'sdffsdfsdfssdfsdf',
    rest: ['', '', '', '', '', '', '', '', '', ''],
    reps: ['', '', '', '', '', '', '', '', '', ''],
    durationBased: true,
    duration: ['00', '30', '00'], // HH:MM:SS
  }
];

function showTotal(total, prefix) {
  let sec_num = parseInt(total, 10); 
  let hours = Math.floor(sec_num / 3600);
  let minutes = Math.floor((sec_num - hours * 3600) / 60);
  let seconds = sec_num - hours * 3600 - minutes * 60;

  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (seconds < 10) {
    seconds = "0" + seconds;
  }
  console.log(`${prefix}: ${hours}:${minutes}:${seconds}`)
  /* return (
      <Paragraph>
        {prefix}: {hours}:{minutes}:{seconds}
      </Paragraph>
    ); */
}

let grandTotal = 0
exercises.map((exercise) => {
  const totalReps = exercise.reps.reduce((a, b) => a + parseInt(b || 0, 10), 0);
  const totalRest = exercise.rest.reduce((a, b) => a + parseInt(b || 0, 10), 0);

  const total = totalReps * 4 + parseInt(totalRest);
  grandTotal += total
  return showTotal(total, `Total ${exercise.title} Duration`);        
})

showTotal(grandTotal, "Grand Total");

休息/代表计算在基于力量的练习中正常工作,但如果我想添加“基于持续时间”的练习,我似乎无法让逻辑起作用。

如果基于持续时间,则存在真或假。

我遇到困难的部分是duration时间计算并将其添加到grandTotal。

标签: javascriptreactjs

解决方案


我猜你reps的时间是几秒钟,所以你需要几秒钟来完成这两种类型的练习。然后你需要将 HH:MM:SS 转换为秒的逻辑,你就完成了

let grandTotal = 0
exercises.map((exercise) => {
let total = 0
if (exercise.durationBased == false) { // your current logic as it works for you
  const totalReps = exercise.reps.reduce((a, b) => a + parseInt(b || 0, 10), 0);
  const totalRest = exercise.rest.reduce((a, b) => a + parseInt(b || 0, 10), 0);

  total = totalReps * 4 + parseInt(totalRest);
} else {
  // guessing that you're accumulating the seconds
  total = 3600 * exercise.duration[0] + 60 * exercise.duration[1] + 1 * exercise.duration[2];
}
  grandTotal += total
  return showTotal(total, `Total ${exercise.title} Duration`); 
})

推荐阅读