-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrencyConverter.html
107 lines (100 loc) · 3.66 KB
/
currencyConverter.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
.converter-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
.converter-container h1 {
margin-bottom: 20px;
}
.converter-container input,
.converter-container select,
.converter-container button {
width: calc(100% - 20px);
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.converter-container .result {
margin-top: 20px;
font-size: 1.2em;
color: #4CAF50;
font-weight: bold;
}
</style>
</head>
<body>
<div class="converter-container">
<h1>Currency Converter</h1>
<input type="number" id="amount" placeholder="" required>
<select id="fromCurrency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="INR">INR</option>
<!-- Add more currencies as needed -->
</select>
<select id="toCurrency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="INR">INR</option>
<!-- Add more currencies as needed -->
</select>
<button id="convertButton">Convert</button>
<div class="result" id="result"></div>
</div>
<script>
let conversionRates;
// Fetch currency conversion rates from an API
async function fetchConversionRates() {
const response = await fetch('https://api.exchangerate-api.com/v4/latest/USD'); // Replace with your API URL
const data = await response.json();
conversionRates = data.rates;
}
// Function to convert currency
function convertCurrency(amount, fromCurrency, toCurrency) {
if (fromCurrency === toCurrency) {
return amount;
}
const fromRate = conversionRates[fromCurrency];
const toRate = conversionRates[toCurrency];
return amount * (toRate / fromRate);
}
// Fetch conversion rates when the page loads
window.onload = fetchConversionRates;
// Event listener for convert button
document.getElementById('convertButton').addEventListener('click', function() {
const amount = parseFloat(document.getElementById('amount').value);
const fromCurrency = document.getElementById('fromCurrency').value;
const toCurrency = document.getElementById('toCurrency').value;
if (isNaN(amount) || amount <= 0) {
alert('Please enter a valid amount');
return;
}
const convertedAmount = convertCurrency(amount, fromCurrency, toCurrency);
const resultContainer = document.getElementById('result');
resultContainer.textContent = `${amount} ${fromCurrency} = ${convertedAmount.toFixed(2)} ${toCurrency}`;
});
</script>
</body>
</html>