Reputation: 193
I have an array with some workout data. And I'm calculating the overall workout duration.
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");
Rest/Reps calculation is working correctly on strength based exercises but if I wanted to add a "Duration based" exercise I cannot seem to get the logic to work.
There is a true or false if it's duration based.
The section I'm having difficulty with is the duration
time calculation and adding that to the grandTotal.
Upvotes: 0
Views: 125
Reputation: 2946
I'm guessing that your reps
is in seconds so you want seconds for both type of exercises. Then you need logic for converting HH:MM:SS to seconds and you're done
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`);
})
Upvotes: 1
Reputation: 1114
You just need to add an if statement that checks the durationBased
property and does the logic a bit different. I more-or-less just copied your logic into the if statement.
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`);
}
})
Upvotes: 0