-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (72 loc) · 3.3 KB
/
script.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
const dropdowns = document.querySelectorAll('.dropdown select'); //This line of code selects all <select> elements within elements that have the class "dropdown" and assigns them to the variable dropdowns.
// console.log(dropdowns); return nodelist with 2 select
const btn = document.querySelector('form button');
const fromCurr = document.querySelector('.from select');
// console.log(fromCurr); //returns object of first select(from)
const toCurr = document.querySelector('.to select'); //returns object of first select(to)
const msg = document.querySelector('.msg');
// for (let code in countryList) {
// console.log(code, countryList[code]); //INR IN
// }
for (let select of dropdowns) {
// console.log(select);
for (currCode in countryList) {
//currCode - INR
let newOption = document.createElement('option');
newOption.innerText = currCode;
newOption.value = currCode;
if (select.name === 'from' && currCode === 'USD') {
newOption.selected = true;
} else if (select.name === 'to' && currCode === 'INR') {
newOption.selected = true;
}
select.append(newOption);
}
select.addEventListener('change', evt => {
//The "change" event is triggered whenever the value of the select element changes. 'evt' , which represents the event object
updateFlag(evt.target); // evt.target refers to the element that triggered the event, in this case, the select element whose value changed.
});
}
const updateFlag = element => {
let currCode = element.value;
// console.log(currCode); //INR
let countryCode = countryList[currCode];
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`;
// console.log(element.parentElement); //select-container
let img = element.parentElement.querySelector('img');
// This statement first accesses the parent element of the element variable.
// Then, it uses querySelector('img') to find the first <img> element that is a descendant of the parent element.
// let img = document.querySelector('.select-container img');
//This statement directly queries the entire document for the first <img> element that is a descendant of an element with the class .select-container.
// console.log(img);
img.src = newSrc;
};
btn.addEventListener('click', evt => {
evt.preventDefault(); //default behavior of form to prevent click event from submitting page
let amount = document.querySelector('.amount input');
// console.log(amount);
let amtVal = amount.value;
console.log(amtVal);
if (amtVal === '' || amtVal < 1) {
amtVal = 1;
console.log(amtVal);
amount.value = amtVal;
}
console.log(fromCurr.value, toCurr.value);
for (const sourceCurrency in exchangeRates) {
const exchangeRate = exchangeRates[sourceCurrency];
// Splitting source and target currencies
const sourceCurrencyCode = sourceCurrency.substring(1); //USD_to_INR
const [source, target] = sourceCurrencyCode.split('_');
// console.log(source);
// console.log(target);
// console.log(sourceCurrencyCode);
// console.log(exchangeRate);
if (source === fromCurr.value && target === toCurr.value) {
let finalAmt = exchangeRate * amtVal;
// console.log(finalAmt);
msg.innerText = `${amtVal}${fromCurr.value} = ${finalAmt}${toCurr.value}`;
break; // Terminate the loop once the desired exchange rate is found
}
}
});