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

Help with a JavaScript algorithm!

Tryin to create a function called clockHandAngles(seconds) that, given the number of seconds since 12:00:00, will print the angles (in degrees) of the hour, minute and second hands. For an input parameter seconds of 119730 (equivalent to 9:15:30 plus 24 hrs!), I am trying to log "Hour Hand: 277.75 degs. Minute Hand: 93 degs. Second Hand: 180 degs.". The angle for the minute hand is not simply 90 degrees; it has advanced a bit further, because of the additional 30 seconds in that minute so far. So far I have got it to display the number of hrs, min, and sec. I can't figure out how to do the rest. Remember everytime it passes the 12 o'clock, or 360 degrees, it resets to 0.

const clockHandAngles = seconds => {
    let counts = [0, 0, 0, seconds];
    let denominations = [3600, 60, 1];
    for(let i = 0; i < 3; i++){    
        counts[i] = Math.floor(counts[3] / denominations[i]);
        counts[3] -= counts[i] * denominations[i];
    }
    console.log(`Hour Hand: ${counts[0]} degs, Minute Hand: ${counts[1]} degs, Second Hand: ${counts[2]} degs`);
};
clockHandAngles(119730)

1 Answer

Steven Parker
Steven Parker
243,318 Points

You probably don't want to apply the floor function in the first calculation, but use it in the second one instead. You also need to apply a scaling factor to convert from units (minutes, seconds, etc) to degrees, and finally limit each value to 360 degrees.

  let scaling = [30, 6, 6];
  for (let i = 0; i < 3; i++) {
    counts[i] = counts[3] / denominations[i] * scaling[i] % 360;
    counts[3] -= Math.floor(counts[i]/scaling[i]) * denominations[i];
  }

Now please tell me I did not just do your homework! :open_mouth:

Steven, you are a lifesaver! I tried a million ways and yes you just did my homework!!! Kidding, but that just alleviated a tad bit of stress!! Thanks again!!!