javascript - 计算整体锻炼时间的“持续时间”
问题描述
我有一个包含一些锻炼数据的数组。我正在计算整体锻炼持续时间。
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。
解决方案
我猜你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`);
})
推荐阅读
- facebook - 如何使用 ffmpeg 为 Facebook 生成循环动画 gif
- json - 如何通过json_query从数组中提取对象?
- python - 如何将二元分类的决策树转换为多标签分类的决策树?
- javascript - 将数据推送到数组后更新 Sate 时,React 功能组件未呈现
- c++ - 如何将分段错误中的信息映射到源代码中的行?
- database-administration - greenplum 的 gp_dump 实用程序不可用
- eclipse - 不可解析的 POM 导致 Maven 构建,安装失败
- python - 使用 kotlin 在 intellij 中运行 python 脚本
- typescript - 打字稿使参数可选
- python - 如何在 Python 中在一个 USB 设备上播放音频并同时在另一个 USB 设备上录制音频?