Skip to content

Instantly share code, notes, and snippets.

@mojenmojen
Created September 26, 2016 00:39
Show Gist options
  • Save mojenmojen/4038d0bb3764d0987fe15da8a0c71324 to your computer and use it in GitHub Desktop.
Save mojenmojen/4038d0bb3764d0987fe15da8a0c71324 to your computer and use it in GitHub Desktop.
Write a method that returns the degree of the angle formed by the two hands of the clock at any given time, to the nearest integer. Remember a circle has 360 degrees. The hour and minute variable are passed in as integers. Note that "the angle between the hands" implies a few things, some or all of which might be tested: 1. we don't care if the …
function clock_angles(hour, minute) {
// note: there are 360 degrees around the clock interface
// calculate the degree position of the hour hand
hourDegree = 30 * hour;
// calculate how far the hour hand is from zero
if ( hourDegree < 180 ) {
hourDifference = hourDegree;
} else {
hourDifference = 360 - hourDegree;
}
// calculate the degree position of the minute hand
// if the minute hand is on the 12 then the degree position should be 0
if ( minute == 60 ) {
minute = 0;
}
// calculate the minutes based on the position
minuteDegree = 6 * minute;
// calculate how far the minute hand is from zero
if ( minuteDegree < 180 ) {
minuteDifference = minuteDegree;
} else {
minuteDifference = 360 - minuteDegree;
}
// calculate the difference between the hour and minute hand
handDifference = hourDifference + minuteDifference;
// return the difference
return handDifference;
}
console.log( "hand angle: (3,0) = " + clock_angles( 3, 0 ));
console.log( "hand angle: (10,10) = " + clock_angles( 10, 10 ));
console.log( "hand angle: (7,10) = " + clock_angles( 7, 10 ));
console.log( "hand angle: (12,59) = " + clock_angles( 12, 59 ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment