Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算;“持续时间”;关于总锻炼时间_Javascript_Reactjs - Fatal编程技术网

Javascript 计算;“持续时间”;关于总锻炼时间

Javascript 计算;“持续时间”;关于总锻炼时间,javascript,reactjs,Javascript,Reactjs,我有一个包含一些训练数据的数组。我在计算总的训练时间 const exercises = [ { title: 'Abdominal Crunch', id: '4YRrftQysvE1Kz8XACtrq4', rest: ['30', '30', '30', '30', '30', '', '', '', '', ''], reps: ['5', '5', '5', '5', '5', '', '', '', '', ''], durationBas

我有一个包含一些训练数据的数组。我在计算总的训练时间

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");
const练习=[
{
标题:“腹部紧缩”,
id:'4YRrftQysvE1Kz8XACtrq4',
其余:['30','30','30','30','30',',
代表:['5','5','5','5','5','5',',
基于持续时间:false,
持续时间:['','',
},
{
标题:“二头肌卷曲”,
id:'SDFFSDFSDFSDFSDF',
其余:['','','','','','','','','','','','','','',
代表:['','','','','','','','','','','','','','','',
基于持续时间的:是的,
持续时间:['00','30','00'],//HH:MM:SS
}
];
函数showTotal(总计,前缀){
设sec_num=parseInt(总计10);
让小时数=数学楼层(秒数/3600);
分钟数=数学楼层(秒数-小时*3600)/60);
设秒数=秒数-小时*3600-分钟*60;
如果(小时<10){
小时数=“0”+小时数;
}
如果(分钟<10){
分钟=“0”+分钟;
}
如果(秒<10){
秒=“0”+秒;
}
log(`${prefix}:${hours}:${minutes}:${seconds}`)
/*返回(
{prefix}:{hours}:{minutes}:{seconds}
); */
}
设grandTotal=0
练习图((练习)=>{
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);
总计+=总计
return showtottal(total,`total${exercise.title}Duration`);
})
showTotal(总计,“总计”);
休息/重复次数的计算在基于力量的练习中是正确的,但是如果我想添加一个“基于持续时间的”练习,我似乎无法让逻辑起作用

如果是基于持续时间的,则有一个true或false


我遇到困难的部分是
持续时间
时间计算,并将其添加到总时间中。

我猜你的
重复次数
以秒为单位,因此两种练习都需要秒。然后需要将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`); 
})

您只需要添加一个if语句,该语句检查
durationBased
属性,并执行稍微不同的逻辑。我或多或少只是将您的逻辑复制到if语句中

exercises.map((exercise) => {
  if (exercise.durationBased) {
    const totalTime = exercise.duration.reduce((a, b) => a + parseInt(b || 0, 10), 0);
    grandTotal += totalTime
    return showTotal(totalTime, `Total ${exercise.title} Duration`);  
  } else {
    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`);  
  }      
})