1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- const urlDefault = 'https://open.er-api.com/v6/latest/USD';
- const currencyMain = document.querySelector('#currencyMain');
- const currencySecond = document.querySelector('#currencySecond');
- const countMain = document.querySelector('#countMain');
- const countSecond = document.querySelector('#countSecond');
- let arrMain = new Map();
- function requestOption (url, currencyMain, currencySecond){
- fetch(url)
- .then(response => response.json())
- .then(data => {
- for (const prod in data.rates) {
- let listOption = document.createElement('option');
- listOption.textContent = prod;
- currencyMain.appendChild(listOption);
- currencySecond.appendChild(listOption.cloneNode(true));
- }
- })
- .catch(er => console.error(er))
- } //отрисовка всех доступных option
- function requestValues (url, arr){
- fetch(url)
- .then(response => response.json())
- .then(data => {
- for (const prod in data.rates) {
- arr.set(prod, data.rates[prod]);
- }
- })
- .catch(er => console.error(er))
- } // получение актуально курса по определённой валюте
- function calculate (countMain, countSecond, currencyMain, currencySecond, arrMain){
- if (currencyMain === currencySecond){
- countSecond.value = 'Выберите разные валюты';
- }
- else if(countMain <= 0){
- countSecond.value = 'Введите число больше 0';
- }
- else {
- countSecond.value = +arrMain.get(currencySecond) * +countMain;
- }
- } // конвертация валют
- function eventListener(arrMain){
- arrMain.clear();
- requestValues(`https://open.er-api.com/v6/latest/${currencyMain.value}`, arrMain);
- setTimeout(() => {
- calculate(countMain.value, countSecond, currencyMain.value, currencySecond.value, arrMain);
- },1000);
- } // функция вызова по событию
- requestOption(urlDefault, currencyMain, currencySecond);
- currencyMain.addEventListener('change', () => eventListener(arrMain));
- currencySecond.addEventListener('change', () => eventListener(arrMain));
- countMain.addEventListener('change', () => eventListener(arrMain));
|