I have a looping task where I need to add the values of certain objects together in an array. While I can successfully log the values individually, like this:
console.log(workouts[0].exercises[0].break);
When I try to incorporate them into a loop, I encounter an error message on the console:
Uncaught TypeError: Cannot read property 'duration' of undefined
If I place the line targeting "break" first, it tells me it cannot read property 'break' of undefined, suggesting that "exercises" may not be defined correctly. However, I am able to log its values without any issues.
How can I ensure that I am able to log the correct value even when it might not be definable? Below is my code snippet:
var workouts = {
"workouts":
[
{
"title": "Full Body",
"exercises":
[
{
"name": "Push Ups",
"duration": 30,
"break": 10
},
{
"name": "Squats",
"duration": 30,
"break": 10
},
{
"name": "Running in Place",
"duration": 30,
"break": 10
}
]
}
]
};
for (var i = 0; i < workouts.length; i++)
{
for (var k = 0; k != workouts.length; k++)
var durations = workouts[i].exercises[k].duration;
var breaks = workouts[i].exercises[k].break;
var totalTime = durations + durations;
console.log(totalTime);
}