-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.61 KB
/
index.js
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
/*
1 meter = 3.281 feet
1 liter = 0.264 gallon
1 kilogram = 2.204 pound
*/
const convertBtn = document.getElementById('convert-btn');
const inputNum = document.getElementById('input');
const lengthPara = document.getElementById('meter-feet');
const volumePara = document.getElementById('litre-gallon');
const massPara = document.getElementById('kilo-pound');
const perMeterToFeet = 3.281;
const perLitreToGallon = 0.264;
const perKiloToPound = 2.204;
const meterToFeet = function (number) {
return number * perMeterToFeet;
}
const feetToMeter = function (number) {
return number / perMeterToFeet;
}
const litreToGallon = function (number) {
return number * perLitreToGallon;
}
const gallonToLitre = function (number) {
return number / perLitreToGallon;
}
const kiloToPound = function (number) {
return number * perKiloToPound;
}
const poundToKilo = function (number) {
return number / perKiloToPound;
}
const conversion = function (number) {
lengthPara.textContent = `${number} meters = ${meterToFeet(number).toFixed(3)} | ${number} feet = ${feetToMeter(number).toFixed(3)}`;
volumePara.textContent = `${number} litres = ${litreToGallon(number).toFixed(3)} | ${number} gallon = ${gallonToLitre(number).toFixed(3)}`;
massPara.textContent = `${number} Kilograms = ${kiloToPound(number).toFixed(3)} | ${number} pounds = ${poundToKilo(number).toFixed(3)}`;
}
convertBtn.addEventListener('click', function () {
try {
const number = Number(inputNum.value);
if (!number) {
throw "not a number";
}
conversion(number);
} catch (err) {
alert(err);
}
})