-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (29 loc) · 1.32 KB
/
script.js
File metadata and controls
36 lines (29 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const hourHand = document.getElementById("hourHand");
const minuteHand = document.getElementById("minuteHand");
const calcBtn = document.getElementById("calcBtn");
const hourInput = document.getElementById("hour");
const minuteInput = document.getElementById("minute");
const angleBetweenText = document.getElementById("angleBetween");
let angleMin = 0;
let angleHour = 0;
const calcAngle = () => {
//reset clock
if (hourInput.value > 12) hourInput.value = 1;
if (minuteInput.value > 59) minuteInput.value = 0;
const hour = parseInt(hourInput.value);
const minute = parseInt(minuteInput.value);
//calc angle for minute hand and hour hand
angleMin = minute * 6;
angleHour = (hour + minute / 60) * 30;
//rotate the clock hands
minuteHand.style.transform = `rotate(${angleMin}deg)`;
hourHand.style.transform = `rotate(${angleHour}deg)`;
//calculate the small angle between the two hands
let angleBetween = Math.abs(angleMin - angleHour);
if (angleBetween > 180) angleBetween = 360 - angleBetween;
//display the angle between the hands on the screen
angleBetweenText.textContent = `the measure of the smaller angle formed by the hands of the clock is: ${angleBetween}`;
};
calcBtn.addEventListener("click", calcAngle);
hourInput.addEventListener("change", calcAngle);
minuteInput.addEventListener("change", calcAngle);