Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Time tracking dashboard - Fetch with JSON file

Konrad 370

@konradbaczyk

Desktop design screenshot for the Time tracking dashboard coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


What are you most proud of, and what would you do differently next time?

I'm proud I was able to fetch data from JSON file. It was my first time to work with that type of file and it doesn't look like professional code for sure, but it works well. Now I want to learn more about fetching data from external sources.

What specific areas of your project would you like help with?

If u know please tell me how I can improve my code in this fragment:

fetch("./data.json")
	.then((res) => res.json())
	.then((data) => (
	(workCurrentTime.textContent = data[0].timeframes.daily.current),
	(workPreviousTime.textContent = data[0].timeframes.daily.previous),
	(playCurrentTime.textContent = data[1].timeframes.daily.current),
	(playPreviousTime.textContent = data[1].timeframes.daily.previous),
	(studyCurrentTime.textContent = data[2].timeframes.daily.current),
	(studyPreviousTime.textContent = data[2].timeframes.daily.previous),
	(exerciseCurrentTime.textContent = data[3].timeframes.daily.current),
	(exercisePreviousTime.textContent = data[3].timeframes.daily.previous),
	(socjalCurrentTime.textContent = data[4].timeframes.daily.current),
	(socjalPreviousTime.textContent = data[4].timeframes.daily.previous),
	(selfCareCurrentTime.textContent = data[5].timeframes.daily.current),
	(selfCarePreviousTime.textContent = data[5].timeframes.daily.previous)
	)
);

I didn't know how to use loop or other staff to minimize proces of looking for data.

Community feedback

@fatemzh

Posted

Congratulations on finishing this challenge, also nice personal touch you added to the activities' cards. To optimize your code further with a loop you could consider gathering the elements into an array first:

const elements = [ { current: workCurrentTime, previous: workPreviousTime }, { current: playCurrentTime, previous: playPreviousTime }, { current: studyCurrentTime, previous: studyPreviousTime }, { current: exerciseCurrentTime, previous: exercisePreviousTime }, { current: socjalCurrentTime, previous: socjalPreviousTime }, { current: selfCareCurrentTime, previous: selfCarePreviousTime } ];

And then, use a loop to iterate over this array and update each element with the corresponding data:

elements.forEach((el, index) => { el.current.textContent = data[index].timeframes.daily.current; el.previous.textContent = data[index].timeframes.daily.previous; });

It's just an idea, hope this helps a bit.

Marked as helpful

0

Konrad 370

@konradbaczyk

Posted

@fatemzh Thank you, I will try implement this next time :)

0

Please log in to post a comment

Log in with GitHub
Discord logo

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