Last active
July 18, 2020 10:17
-
-
Save clarketm/14b228d3ff5224625b9fea2c85764905 to your computer and use it in GitHub Desktop.
Clock Angle Problem (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getClockAngle(hours, minutes, isPortionWithTwelve) { // using 12-hour OR 24-hour clock notation | |
hours = hours % 12; | |
var hourMinPart = 0.5 * minutes, // 30 degrees per 60 minutes => 1/2 degree per 1 minute => 0.5 * minute | |
hourHourPart = 30 * hours, // 30 degrees per 1 hour => 30 * hour | |
minAngle = 6 * minutes, // 360 degrees per 60 minutes => 6 degrees per 1 minute => 6 * minute | |
totalAngle = Math.abs(hourMinPart + hourHourPart - minAngle); // absolute difference | |
return isPortionWithTwelve ? 360 - totalAngle : totalAngle; // subtract the total angle from 360 to get the portion w/ 12 | |
} | |
// Examples | |
console.log("4:42", getClockAngle(4, 42)); // 111 degrees | |
console.log("4:42", getClockAngle(4, 42, true)); // 249 degrees | |
console.log("12:01", getClockAngle(12, 01)); // 5.5 degrees | |
console.log("12:00", getClockAngle(12, 00)); // 0 degrees | |
console.log("00:01", getClockAngle(00, 01)); // 5.5 degrees | |
console.log("24:01", getClockAngle(24, 01)); // 5.5 degrees | |
console.log("16:42", getClockAngle(16, 42)); // 111 degrees | |
console.log("11:01", getClockAngle(11, 01)); // 324.5 degrees |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment