-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (62 loc) · 2.16 KB
/
script.js
File metadata and controls
76 lines (62 loc) · 2.16 KB
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
let fromSelect = document.getElementById("fromCurrency")
let toSelect = document.getElementById("toCurrency")
let amountInput = document.getElementById("amount")
let btn = document.getElementById("convertBtn")
async function currency() {
let response = await fetch("https://api.exchangerate-api.com/v4/latest/USD")
let data = await response.json()
let currencies = Object.keys(data.rates)
currencies.forEach(currency => {
let option1 = document.createElement("option")
option1.value = currency
option1.textContent = currency
let option2 = document.createElement("option")
option2.value = currency
option2.textContent = currency
if (currency === "INR") {
option1.selected = true;
}
if (currency === "USD") {
option2.selected = true;
}
fromSelect.append(option1)
toSelect.append(option2)
});
}
currency()
fromSelect.addEventListener("change",(evt)=>{
upadateFlag(evt.target)
})
toSelect.addEventListener("change",(evt)=>{
upadateFlag(evt.target)
})
const upadateFlag = (element) =>{
let currCode = element.value
let countryCode = countryList[currCode]
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`
if (element.id === "fromCurrency") {
document.getElementById("fromImg").src = newSrc;
} else if (element.id === "toCurrency") {
document.getElementById("toImg").src = newSrc;
}
}
btn.addEventListener("click", async ()=>{
let amount = amountInput.value
let fromCurr = document.getElementById("fromCurrency").value
let toCurr = document.getElementById("toCurrency").value
try{
if(!amount || amount < 1)
throw new Error("Amount should not be empty or less than 1");
let response = await fetch(`https://api.exchangerate-api.com/v4/latest/${fromCurr}`)
if(!response.ok)
throw new Error("Internet or API issue")
let data = await response.json()
let rate = data.rates[toCurr]
if(!rate)
throw new Error("Currency rate not found")
let result = amount * rate
document.getElementById("result").innerHTML = `${amount} ${fromCurr} = ${result} ${toCurr}`
}catch(err){
document.getElementById("result").textContent = err.message
}
})