forked from namankumarjangid/JS-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempJSconverter.html
122 lines (99 loc) · 3.08 KB
/
tempJSconverter.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>temp converter</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Mulish", sans-serif;
}
body {
width: 100vw;
height: 100vh;
background-color: #fff;
display: grid;
place-items: center;
}
form {
background-color: #fab1a0;
padding: 80px;
width: 50%;
margin: 100px auto;
text-align: center;
-webkit-box-shadow: 0 10px 6px -6px #777;
-moz-box-shadow: 0 10px 6px -6px #777;
box-shadow: 0 10px 6px -6px #777;
}
#tempCalc label {
font-size: 30px;
}
#temp {
border: none;
padding: 10px;
height: 40px;
width: 100px;
font-size: 20px;
margin-top: 20px;
}
#temp_diff {
height: 40px;
border: none;
margin-top: 20px;
font-size: 15px;
vertical-align: top;
}
[type="submit"] {
height: 35px;
margin: 20px;
width: 100px;
font-size: 15px;
border: none;
background-color: black;
color: white;
}
#resultContainer {
font-size: 35px;
}
</style>
</head>
<body>
<form id="tempCalc" onsubmit="calculateTemp(); return false">
<label for="temp">Please enter the temperature you like to convert</label>
<br /> <input type="number" name="temp" id="temp">
<select name="temp_diff" id="temp_diff">
<option value="cel">°Celsius</option>
<option value="fah">°Fahrenheit</option>
</select> <br />
<input type="submit" name="temp"> <br />
<span id="resultContainer"></span>
</form>
<script>
const calculateTemp = () => {
const numberTemp = document.getElementById('temp').value;
// console.log(numberTemp);
const tempSelected = document.getElementById('temp_diff');
const valueTemp = temp_diff.options[tempSelected.selectedIndex].value;
const celToFah = (cel) => {
let fahrenheit = Math.round((cel * 9 / 5) + 32);
return fahrenheit;
}
const fehToCel = (fehr) => {
let celsius = Math.round((fehr - 32) * 5 / 9);
return celsius;
}
let result;
if (valueTemp == 'cel') {
result = celToFah(numberTemp);
document.getElementById('resultContainer').innerHTML = `= ${result}°Fahrenheit`;
} else {
result = fehToCel(numberTemp);
document.getElementById('resultContainer').innerHTML = `= ${result}°Celsius`;
}
}
</script>
</body>
</html>