-
Notifications
You must be signed in to change notification settings - Fork 0
/
forex.js
89 lines (73 loc) · 3.63 KB
/
forex.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
document.addEventListener('DOMContentLoaded', () => {
const rates = {
USD: { CAD: 1.25, AUD: 1.30, CNY: 6.50 },
CAD: { USD: 0.80, AUD: 1.04, CNY: 5.20 },
AUD: { USD: 0.77, CAD: 0.96, CNY: 4.95 },
};
function displayRates() {
let table = "<table><tr><th>From</th><th>To</th><th>Rate</th></tr>";
for (let fromCurrency in rates) {
for (let toCurrency in rates[fromCurrency]) {
table += `<tr><td>${fromCurrency}</td><td>${toCurrency}</td><td>${rates[fromCurrency][toCurrency]}</td></tr>`;
}
}
table += "</table>";
document.getElementById("currentRates").innerHTML = table;
}
document.getElementById('add-rate-form').addEventListener('submit', (event) => {
event.preventDefault();
const fromCurrency = document.getElementById('fromCurrency').value.toUpperCase();
const toCurrency = document.getElementById('toCurrency').value.toUpperCase();
const rate = parseFloat(document.getElementById('rate').value);
if (!rates[fromCurrency]) {
rates[fromCurrency] = {};
}
rates[fromCurrency][toCurrency] = rate;
console.log('Updated exchange rates:', rates);
alert(`Added: 1 ${fromCurrency} = ${rate} ${toCurrency}`);
displayRates();
document.getElementById('add-rate-form').reset();
});
document.getElementById('convert-currency-form').addEventListener('submit', (event) => {
event.preventDefault();
const amount = parseFloat(document.getElementById('convert-amount').value);
const sourceCurrency = document.getElementById('sourceCurrency').value.toUpperCase();
const destinationCurrency = document.getElementById('destinationCurrency').value.toUpperCase();
if (isNaN(amount) || amount <= 0) {
document.getElementById("result").innerText = "Please enter a valid amount.";
return;
}
if (!rates[sourceCurrency] || !rates[sourceCurrency][destinationCurrency]) {
document.getElementById("result").innerText = "Conversion rate not available.";
return;
}
const rate = rates[sourceCurrency][destinationCurrency];
const convertedAmount = amount * rate;
const now = new Date();
const date = now.toDateString();
const hours = now.getHours();
const minutes = now.getMinutes();
document.getElementById("result").innerText = `${amount} ${sourceCurrency} = ${convertedAmount.toFixed(4)} ${destinationCurrency}, ${date}, ${hours}:${minutes}`;
document.getElementById('convert-currency-form').reset();
});
document.getElementById('update-rate-form').addEventListener('submit', (event) => {
event.preventDefault();
const fromCurrency = document.getElementById('updateFromCurrency').value.toUpperCase();
const toCurrency = document.getElementById('updateToCurrency').value.toUpperCase();
const newRate = parseFloat(document.getElementById('newRate').value);
if (!rates[fromCurrency] || !rates[fromCurrency][toCurrency]) {
alert("The specified currency conversion does not exist.");
return;
}
if (isNaN(newRate)) {
alert("Please enter a valid number for the rate.");
return;
}
rates[fromCurrency][toCurrency] = newRate;
console.log('Updated exchange rates:', rates);
alert(`Updated: 1 ${fromCurrency} = ${newRate} ${toCurrency}`);
displayRates();
document.getElementById('update-rate-form').reset();
});
displayRates();
});