-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (36 loc) · 1.05 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
const form = document.querySelector("form");
const fromCurrency = document.querySelector("#from-currency");
const toCurrency = document.querySelector("#to-currency");
const amount = document.querySelector("#amount");
const result = document.querySelector("#result span");
form.addEventListener("submit", function(event) {
event.preventDefault();
if (!amount.value || isNaN(amount.value)) {
result.textContent = "Invalid amount";
return;
}
// fetch exchange rate from function
const exchangeRate = getExchangeRate(fromCurrency.value, toCurrency.value);
const convertedAmount = amount.value * exchangeRate;
result.textContent = convertedAmount.toFixed(2);
});
function getExchangeRate(from, to) {
if (from === "USD" && to === "EUR") {
return 0.86;
}
if (from === "USD" && to === "KSH") {
return 107.36;
}
if (from === "KSH" && to === "USD") {
return 0.00932;
}
if (from === "EUR" && to === "USD") {
return 1.07;
}
if (from === "EUR" && to === "KSH") {
return 133.53;
}
if (from === "KSH" && to === "EUR") {
return 0.0075;
}
}