I am currently working on a challenge to calculate the total number of likes. However, I am encountering an issue where I am receiving an error message stating that the posts do not exist. The user object contains a property called posts, which is an array of objects. Can anyone provide guidance on how to correctly access the likes property within this structure? Thank you.
//Initialize the sum variable
var sum = 0;
//Create a user object with posts property
var user = {
posts: [
{
likes: 10
},
{
likes: 15
}
]
}
//Iterate through the posts array and calculate the total sum of likes
for (i = 0; i < posts.length; i++) {
sum += user.posts[i].likes;
}
return sum;