Time Tracking Dashboard

Solution retrospective
I am unable to loop over each class; I am only able to change the first of type upon clicking. I understand that querySelectorAll should be used with forEach, but I am unsure how to include it with my current code. I am still learning JavaScript so I need a lot of guidance. Please correct my mistakes and let me know what else I can do to make it work
Please log in to post a comment
Log in with GitHubCommunity feedback
- @alberto-rj
👋 Hi yinnie, good to see you back solving the challenges!
👏 Congrats on your solution, I found your code very easy to read!
Why this is happening
On line 24 to 26 of your
script.js
file, using thedocument.querySelector()
method, you are only getting the firsth2
,.hours
and.prev
elements of the DOM.My suggestion for the problem
Step 1: Starts at line 5 of your
script.js
file, get the six cards of the DOM.// card containers const workContainer = document.querySelector(".workContainer"); const playContainer = document.querySelector(".playContainer"); const studyContainer = document.querySelector(".studyContainer"); const exerciseContainer = document.querySelector(".exerciseContainer"); const socialContainer = document.querySelector(".socialContainer"); const selfCareContainer = document.querySelector(".selfCareContainer");
Step 2: After that, create a cards object, where each key is associated with the title of each card.
// card map const cardMap = { "Work": workContainer, "Play": playContainer, "Study": studyContainer, "Exercise": exerciseContainer, "Social": socialContainer, "Self Care": selfCareContainer };
Step 3: To work as expected, your
updateTime
function must be implemented as follows:function updateTime (timeFrame) { // getting JSON fetch("data.json") .then(response => response.json()) .then(data => { data.forEach(value => { // store json data in variables const current = value.timeframes[timeFrame].current; const previous = value.timeframes[timeFrame].previous; const title = value.title; // get the card container by title const cardContainer = cardMap[title]; // get the children of the card container const titleElement = cardContainer.querySelector("h2"); const hrElement = cardContainer.querySelector(".hours"); const prevElement = cardContainer.querySelector(".prev"); // populate the children of the card container titleElement.innerText = title; hrElement.innerText = current + "hrs"; if (timeFrame == "daily") { prevElement.innerText = `Yesterday - ${previous} hrs`; } else if (timeFrame == "weekly") { prevElement.innerText = `Last Week - ${previous} hrs`; } else { prevElement.innerText = `Last Month - ${previous} hrs`; } }) }) .catch(err => console.log(err)); }
Final words
Note, there are many other ways to approach this same problem. I chose this way because I thought it would be the easiest to understand. If anything is unclear, or if you have any questions, let me know. It would be a pleasure to exchange ideas.
Marked as helpful
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord