Skip to content

Commit 5d3a3fb

Browse files
authored
Create index.html
1 parent 84e5bdd commit 5d3a3fb

File tree

1 file changed

+120
-0
lines changed
  • Other_Example/Assignment Exercises/Temperature Converter

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Temperature Converter</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
max-width: 500px;
10+
margin: 0 auto;
11+
padding: 20px;
12+
text-align: center;
13+
background-color: #f4f4f4;
14+
}
15+
.container {
16+
background-color: white;
17+
border-radius: 10px;
18+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
19+
padding: 30px;
20+
}
21+
input, select, button {
22+
margin: 10px 0;
23+
padding: 10px;
24+
width: 100%;
25+
box-sizing: border-box;
26+
}
27+
#result {
28+
margin-top: 20px;
29+
font-weight: bold;
30+
color: #333;
31+
}
32+
button {
33+
background-color: #007bff;
34+
color: white;
35+
border: none;
36+
border-radius: 5px;
37+
cursor: pointer;
38+
transition: background-color 0.3s ease;
39+
}
40+
button:hover {
41+
background-color: #0056b3;
42+
}
43+
</style>
44+
</head>
45+
<body>
46+
<div class="container">
47+
<h1>Temperature Converter</h1>
48+
<input
49+
type="number"
50+
id="temperatureInput"
51+
placeholder="Enter temperature"
52+
>
53+
<select id="fromUnit">
54+
<option value="C">Celsius (°C)</option>
55+
<option value="F">Fahrenheit (°F)</option>
56+
</select>
57+
<select id="toUnit">
58+
<option value="F">Fahrenheit (°F)</option>
59+
<option value="C">Celsius (°C)</option>
60+
</select>
61+
<button onclick="convertTemperature()">Convert</button>
62+
<div id="result"></div>
63+
</div>
64+
65+
<script>
66+
// Temperature Conversion Functions
67+
function celsiusToFahrenheit(celsius) {
68+
return (celsius * 9/5) + 32;
69+
}
70+
71+
function fahrenheitToCelsius(fahrenheit) {
72+
return (fahrenheit - 32) * 5/9;
73+
}
74+
75+
function convertTemperature() {
76+
// Get input values
77+
const temperatureInput = document.getElementById('temperatureInput');
78+
const fromUnitSelect = document.getElementById('fromUnit');
79+
const toUnitSelect = document.getElementById('toUnit');
80+
const resultDiv = document.getElementById('result');
81+
82+
// Validate input
83+
const temperature = parseFloat(temperatureInput.value);
84+
if (isNaN(temperature)) {
85+
resultDiv.textContent = 'Please enter a valid number';
86+
resultDiv.style.color = 'red';
87+
return;
88+
}
89+
90+
const fromUnit = fromUnitSelect.value;
91+
const toUnit = toUnitSelect.value;
92+
93+
// Perform conversion
94+
let convertedTemp, resultText;
95+
if (fromUnit === 'C' && toUnit === 'F') {
96+
convertedTemp = celsiusToFahrenheit(temperature);
97+
resultText = `${temperature}°C = ${convertedTemp.toFixed(2)}°F`;
98+
} else if (fromUnit === 'F' && toUnit === 'C') {
99+
convertedTemp = fahrenheitToCelsius(temperature);
100+
resultText = `${temperature}°F = ${convertedTemp.toFixed(2)}°C`;
101+
} else {
102+
// If units are the same, just display the input
103+
convertedTemp = temperature;
104+
resultText = `${temperature}°${fromUnit}`;
105+
}
106+
107+
// Display result
108+
resultDiv.textContent = resultText;
109+
resultDiv.style.color = 'green';
110+
}
111+
112+
// Allow conversion on pressing Enter key
113+
document.getElementById('temperatureInput').addEventListener('keypress', function(event) {
114+
if (event.key === 'Enter') {
115+
convertTemperature();
116+
}
117+
});
118+
</script>
119+
</body>
120+
</html>

0 commit comments

Comments
 (0)