-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (104 loc) · 4.03 KB
/
script.js
File metadata and controls
125 lines (104 loc) · 4.03 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Tab switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
const tabName = btn.dataset.tab;
// Remove active class from all tabs and contents
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
// Add active class to clicked tab and corresponding content
btn.classList.add('active');
document.getElementById(tabName).classList.add('active');
});
});
// Length conversion
const lengthRatios = {
mm: 0.001,
cm: 0.01,
m: 1,
km: 1000,
inch: 0.0254,
ft: 0.3048,
yard: 0.9144,
mile: 1609.34
};
function convertLength() {
const input = parseFloat(document.getElementById('length-input').value);
const fromUnit = document.getElementById('length-from').value;
const toUnit = document.getElementById('length-to').value;
if (isNaN(input)) {
document.getElementById('length-output').value = '';
return;
}
const meters = input * lengthRatios[fromUnit];
const result = meters / lengthRatios[toUnit];
document.getElementById('length-output').value = result.toFixed(6);
}
document.getElementById('length-input').addEventListener('input', convertLength);
document.getElementById('length-from').addEventListener('change', convertLength);
document.getElementById('length-to').addEventListener('change', convertLength);
// Weight conversion
const weightRatios = {
mg: 0.000001,
g: 0.001,
kg: 1,
ton: 1000,
oz: 0.0283495,
lb: 0.453592
};
function convertWeight() {
const input = parseFloat(document.getElementById('weight-input').value);
const fromUnit = document.getElementById('weight-from').value;
const toUnit = document.getElementById('weight-to').value;
if (isNaN(input)) {
document.getElementById('weight-output').value = '';
return;
}
const kg = input * weightRatios[fromUnit];
const result = kg / weightRatios[toUnit];
document.getElementById('weight-output').value = result.toFixed(6);
}
document.getElementById('weight-input').addEventListener('input', convertWeight);
document.getElementById('weight-from').addEventListener('change', convertWeight);
document.getElementById('weight-to').addEventListener('change', convertWeight);
// Temperature conversion
function convertTemperature() {
const input = parseFloat(document.getElementById('temp-input').value);
const fromUnit = document.getElementById('temp-from').value;
const toUnit = document.getElementById('temp-to').value;
if (isNaN(input)) {
document.getElementById('temp-output').value = '';
return;
}
let celsius;
if (fromUnit === 'c') {
celsius = input;
} else if (fromUnit === 'f') {
celsius = (input - 32) * 5/9;
} else if (fromUnit === 'k') {
celsius = input - 273.15;
}
let result;
if (toUnit === 'c') {
result = celsius;
} else if (toUnit === 'f') {
result = celsius * 9/5 + 32;
} else if (toUnit === 'k') {
result = celsius + 273.15;
}
document.getElementById('temp-output').value = result.toFixed(4);
}
document.getElementById('temp-input').addEventListener('input', convertTemperature);
document.getElementById('temp-from').addEventListener('change', convertTemperature);
document.getElementById('temp-to').addEventListener('change', convertTemperature);
// Swap units function
function swapUnits(type) {
const fromSelect = document.getElementById(`${type === 'temperature' ? 'temp' : type}-from`);
const toSelect = document.getElementById(`${type === 'temperature' ? 'temp' : type}-to`);
const temp = fromSelect.value;
fromSelect.value = toSelect.value;
toSelect.value = temp;
// Trigger conversion
if (type === 'length') convertLength();
else if (type === 'weight') convertWeight();
else if (type === 'temperature') convertTemperature();
}