JSFiddle - React, Tailwind, and code Playground
by Jake Overall
JavaScript
/*
Given a standard clock face write a function that will return the inner angle between between the hour and minute hands. For instance
12:00 // returns 0
03:00 // returns 90
12:30 // returns 165
***Remember the hour hand will move along with each minute and we are looking for the INNER *AKA Smallest Angle
*/
var clock = function (h, m) {
var anglePerMin = 360 / 60;
var hAngle = h * 60; //Angle at the exact hour
hAngle += m; // how many min have elapsed
hAngle *= 0.5; // the hour hand moves half the distance of the elapsed mins
var mAngle = m * anglePerMin;
var angle = Math.abs(hAngle - mAngle); // Gets the angle between the two hands but could be the outer angle if the hour hand is past the min hand
var ans = Math.min(360 - angle, angle); // This will make sure you always return the inner angle or the smallest angle
return ans + ' Degrees';
};
alert(clock(12, 30));