Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

Time tracking dashboard

Shane Chaffe•900
@Chaffexd
A solution to the Time tracking dashboard challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


How can I clean up my JS on this?

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Elaine•11,360
    @elaineleung
    Posted almost 3 years ago

    Hi Shane, first off, good job in building this dashboard! About the JS, the key here is really to use iteration for elements with the same style and content type, and for me I usually use forEach (a for loop works fine also). You can use this for both the timeframe labels (daily, weekly, monthly) and also for the six stats cards, but here I'll just show you what you can do with the cards:

    1. Instead of assigning an id for each area and then grabbing each one in your script, I would give them a class of .card and then use querySelectorAll to select all the cards at once:

        const allCards = document.querySelectorAll(".card");
      
    2. In the HTML, add some classes to the elements showing the "previous" and "current" stats:

      <div class="item2 card">
        <div class="work"></div>
        <div class="workTime">
          <div class="title">
            <p class="workTitle">Work</p>
            <p class="dotIcon"><a href="#">...</a></p>
          </div>
          <div class="timeRecorded stats">
            <p class="stats-current">4hrs</p>
            <p class="stats-previous">Last Week - 5hrs</p>
          </div>
        </div>
      </div>
      
    3. After that, you can loop through allCards like this:

      daily.addEventListener("click" , () => { 
         daily.classList.add("active");
         weekly.classList.remove("active");
         monthly.classList.remove("active");
      
         allCards.forEach( (card, idx) => {
         const currentStats = card.querySelector(".stats-current")
         const previousStats = card.querySelector(".stats-previous")
      
         currentStats.textContent = `${data[idx].timeframes.daily.current}hrs`;
         previousStats.textContent = `Yesterday - ${data[idx].timeframes.daily.previous}hrs`;
         })
      })
      

    That's essentially all there is! Another suggestion I can give you is, instead of using a for the clickable timeframe label element, use a button or a similar kind of input element instead (I used radio inputs in mine). The reason for this is, the function of a link is for website navigation, but here, the element is being used to perform an action of changing information and not actually directing the user to another page. It's very common to see a button being used for a link (like the "call to action" links) or a link being used as a button like in this case, and if not used properly, this is something that can give you accessibility issues.

    Hope this helps you a bit!

    Marked as helpful
  • Brannon•80
    @ekkas303
    Posted almost 3 years ago

    hey bro just want to ask how did you connect json file to javascript because i did paste this: fetch("/data.json") .then((response) => { return response.json(); }) this is what i got : File "c:\Users\5750G\Desktop\time-tracking-dashboard-main\index.js", line 6 .then((response) => { ^ SyntaxError: invalid syntax PS C:\Users\5750G.vscode> Any idea why its not working?

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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub