123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // Задание на синий пояс
- // Сделать задания обмена валют используя ассоциативный массив (объект) подобной структуры. Добавьте дополнительные поля при надобности. Для обращения к нужному полю используйте [].
- // let ratios = {
- // usd: 25.6,
- // eur: 29
- // }
- // real data
- // Иcпользуя заготовку ниже реализуйте перевод валют с реальными данными.
- // fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- // .then(data => {
- // console.log(data.rates.UAH)
-
- // })
- const f12 = (rootId) => {
- const task12block = document.createElement('div');
- const task12_1title = document.createElement('h2');
- task12_1title.innerText = 'Task-12-1 Сurrency calc blue belt';
- const exchange_12_1Btn = document.createElement('button');
- exchange_12_1Btn.innerText = "Exchange UAH to USD or EUR";
- exchange_12_1Btn.style = 'margin-bottom:20px';
- rootId.appendChild(task12block);
- task12block.appendChild(task12_1title);
- task12block.appendChild(exchange_12_1Btn);
- let ratios = {
- usd: 25.6,
- eur: 29
- }
- exchange_12_1Btn.onclick = () => {
- const toCurrency = prompt('Введите валюту, которую хотите купить: EUR или USD');
- let fromAmount=null;
- let toAmount = null;
- if (toCurrency) {
- switch (toCurrency.toLowerCase()) {
- case 'usd':
- fromAmount = prompt('Введите сумму в гривне');
- toAmount = fromAmount / ratios['usd'];
- break;
- case 'eur':
- fromAmount = prompt('Введите сумму в гривне');
- toAmount = fromAmount / ratios['eur'];
- break;
- default: alert('Чет я не понял, попробуйте еще раз');
- break;
- }
-
- !fromAmount || alert(`На ${fromAmount} UAH вы можете приобрести ${Math.round(toAmount * 100) / 100} ${toCurrency.toUpperCase()} `);
- }
- else alert('Чет я не понял, попробуйте еще раз');
- }
-
- const task12_2title = document.createElement('h2');
- task12_2title.innerText = 'Task-12-2 Сurrency calc: real data';
- const exchange_12_2Btn = document.createElement('button');
- exchange_12_2Btn.innerText = "Exchange UAH to USD or EUR";
- exchange_12_2Btn.style = 'margin-bottom:20px';
- task12block.appendChild(task12_2title);
- task12block.appendChild(exchange_12_2Btn);
- exchange_12_2Btn.onclick = () => {
- let toCurrency = prompt('Введите валюту, которую хотите купить: EUR или USD');
- let fromAmount=null;
- let toAmount = null;
- let currencyRatio = null;
- if (toCurrency) {
- toCurrency = toCurrency.toLowerCase();
- if (toCurrency === 'usd' || toCurrency === 'eur') {
- fromAmount = prompt('Введите сумму в гривне');
- fetch(`https://open.er-api.com/v6/latest/${toCurrency}`).then(res => res.json())
- .then(data => {
- toAmount = fromAmount / (data.rates.UAH);
- !fromAmount || alert(`На ${fromAmount} UAH вы можете приобрести ${Math.round(toAmount * 100) / 100} ${toCurrency.toUpperCase()} `)
- })
- }
- else alert('Чет я не понял, попробуйте еще раз');
- }
- else alert('Чет я не понял, попробуйте еще раз');
- }
- }
- f12(root);
- export default f12;
|