Can I make this function cleaner by using not forEach() but .reduce() please?
const post = [
{
id: "1",
dateOfPost: "15.10.2020",
postTitle: "...",
postText: "...",
},
comments: [
{
id: "1",
dateOfComment: "15.10.2020",
gravatar: "...",
nicName: "...",
commentText: "...",
starRating: 3,
likeCount: 8,
dislikeCount: 1
},
{
id: "2",
dateOfComment: "15.10.2020",
gravatar: "...",
nicName: "...",
commentText: "...",
starRating: 5,
likeCount: 1,
dislikeCount: 1
},
]
}
];
const postComments = post[0].comments;
const starRatingAverage = () => {
let starRatingAVG = 0;
postComments.forEach(comment => {
starRatingAVG = starRatingAVG + comment.starRating
})
return starRatingAVG = starRatingAVG / postComments.length;
}
I'm just a beginner and I'm training to do things right. So I will be grateful for all your help. Thanks.