Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Your though guys

The code is based on this tutoria: https://teamtreehouse.com/library/using-anonymous-functions-with-setinterval

const clockSection2 = document.getElementById("clock2");
function clock(){
  //Create a function  to append 0 to hours less than 10
  function zero(hrs){
    if (hrs < 10){
      //append 0 next to the number
      return "0" + hrs;
    }
    else{
      return hrs;
    }
  }

  const now = new Date();
  const hh = zero(now.getHours());
  const mm = zero(now.getMinutes());
  const ss = zero(now.getSeconds());

  return `${hh}:${mm}:${ss}`;
}
let initCallback = () => clockSection2.textContent = `${clock()}`;

initCallback();
setInterval((initCallback), 1000);

1 Answer

Steven Parker
Steven Parker
231,248 Points

Not sure what you mean by "though" here. Did you intend to ask a question about this code, or are you just sharing? Either way, you might want to include the HTML portion of the code.

I'm guessing it's the latter since the code seems perfectly functional, and I'll contribute to it with this alternate and more compact way to define the "zero" function:

  const zero = hrs => ("0" + hrs).slice(-2);