here is my problem: When I click on a like, it is incremented by 1. This works for all photos that can be liked. I would like to add the total number of likes based on user clicks. I already have my ternary function to add or remove the like but I can't seem to sum the total likes and display it in the console. I can't find the solution because inside my event I don't have access to the total of likable photos (just one of each) and if I leave my event I don't have access to my incrementation function ... Can you help me ? Thanks in advance !
PS : I specify that at the base there is already a number of likes displayed which depends on each photo (data coming from a json file), the user adds his like or removes it by clicking on it.
// incrementation likes
const likeContainers = document.querySelectorAll(".like-container");
likeContainers.forEach((likeContainer) => {
let tmpLike = Number(likeContainer.childNodes[0].innerHTML);
likeContainer.addEventListener("click", (e) => {
const g = likeContainer.childNodes[0].innerHTML;
const like = Number(g);
const incrementLikePhoto =
like === tmpLike ? like + 1 : tmpLike;
likeContainer.childNodes[0].innerHTML = incrementLikePhoto;
});
});
//likes sum
const reducer = (acc, curr) => acc + curr;
let sum = findTmpLikes.reduce(reducer);
const totalPriceContainer = document.createElement("div");
totalPriceContainer.setAttribute("classe", "total-price__container");
totalPriceContainer.append(sum)
stickyCard.append(totalPriceContainer);