-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (28 loc) · 1.33 KB
/
script.js
File metadata and controls
33 lines (28 loc) · 1.33 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
const inputValue = document.getElementById('input-value');
const convertBtn = document.getElementById('convert-btn');
const lengthEl = document.getElementById('results-1');
const volumeEl = document.getElementById('results-2');
const massEl = document.getElementById('results-3');
const meterToFeet = 3.28084;
const literToGallon = 0.264172;
const kilogramToPound = 2.20462;
function convertUnits() {
const value = parseFloat(inputValue.value);
if (!isNaN(value)) {
const meters = value;
const feet = value * meterToFeet;
lengthEl.textContent = `${value} meters = ${feet.toFixed(3)} feet | ${value} feet = ${(meters/meterToFeet).toFixed(3)} meters`;
const liters = value;
const gallons = value * literToGallon;
volumeEl.textContent = `${value} liters = ${gallons.toFixed(3)} gallons | ${value} gallons = ${(liters/literToGallon).toFixed(3)} liters`;
const kilograms = value;
const pounds = value * kilogramToPound;
massEl.textContent = `${value} kilograms = ${pounds.toFixed(3)} pounds | ${value} pounds = ${(kilograms/kilogramToPound).toFixed(3)} kilograms`;
}
}
convertBtn.addEventListener('click', convertUnits);
inputValue.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
convertUnits();
}
});