-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (105 loc) · 2.42 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
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
/* requires : follow design spec, generate all coversion when the users click "convert", round the number down to three decimals */
let inputEl = document.querySelector("#input-el")
let length = document.querySelector("#length")
let volume = document.querySelector("#volume")
let mass = document.querySelector("#mass")
const button = document.querySelector("#convert-btn")
let feet = 3.281
let gallon = 0.264
let kilo = 2.204
// Button Convert clicked :
const clicked = button.addEventListener("click", ()=>{
validateInput(inputEl)
})
// Validates if the input is a number
function validateInput(a) {
a = document.querySelector("#input-el").value
if (isNaN(a) || a < 0) {
inputEl = "invalid"
console.log("invalid input")
alert("Please use digits only.")
} else {
inputEl = Number(a)
showConversion()
}
console.log(typeof inputEl)
}
// Fill the divs with each conversion (Length, Volume, Mass)
function showConversion() {
conversion(inputEl, feet, length)
conversion(inputEl, gallon, volume)
conversion(inputEl, kilo, mass)
console.log("converted")
}
// Conversion
function conversion(a, b, c) {
let el = ""
let el2 = ""
let result = 0
result = a * b;
let input1 = result.toFixed(3)
result = a / b
let input2 = result.toFixed(3)
if (b === 3.281) {
if (a == 1) {
el = "meter"
el2 = "foot"
} else {
el = "meters"
el2 = "feet"
}
} else if (b === 0.264) {
if (a == 1) {
el = "liter"
el2 = "gallon"
} else{
el = "liters"
el2 = "gallons"
}
} else {
if (a == 1) {
el = "kilo"
el2 = "pound"
} else {
el = "kilos"
el2 = "pounds"
}
}
return c.innerHTML = `${a} ${el} = ${input1} ${el2} | ${a} ${el2} = ${input2} ${el}`
}
// Conversion
function conversion(a, b, c) {
let el = ""
let el2 = ""
let result = 0
result = a * b;
let input1 = result.toFixed(3)
result = a / b
let input2 = result.toFixed(3)
if (b === 3.281) {
if (a == 1) {
el = "meter"
el2 = "foot"
} else {
el = "meters"
el2 = "feet"
}
} else if (b === 0.264) {
if (a == 1) {
el = "liter"
el2 = "gallon"
} else{
el = "liters"
el2 = "gallons"
}
} else {
if (a == 1) {
el = "kilo"
el2 = "pound"
} else {
el = "kilos"
el2 = "pounds"
}
}
return c.innerHTML = `${a} ${el} = ${input1} ${el2} | ${a} ${el2} = ${input2} ${el}`
}