1function formatToCurrency(amount){
2 return (amount).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
3}
4formatToCurrency(12.34546); //"12.35"
5formatToCurrency(42345255.356); //"42,345,255.36"
1const select = document.querySelectorAll('.currency');
2 const number = document.getElementById("number");
3 const output = document.getElementById("output");
4
5
6 fetch('https://api.frankfurter.app/currencies').then((data) => data.json())
7 .then((data) => {
8 display(data);
9 });
10
11
12 function display(data) {
13 const entries = Object.entries(data);
14 for (var i = 0; i < entries.length; i++) {
15 select[0].innerHTML += `<option value="${entries[i][0]}">${entries[i][0]} : ${entries[i][1]}</option>`;
16 select[1].innerHTML += `<option value="${entries[i][0]}">${entries[i][0]} : ${entries[i][1]}</option>`;
17 }
18 }
19
20
21
22 function updatevalue() {
23 let currency1 = select[0].value;
24 let currency2 = select[1].value;
25
26 let value = number.value;
27
28
29 if (currency1 != currency2) {
30 convert(currency1, currency2, value);
31 } else {
32 alert("Choose Diffrent Currency");
33 }
34 }
35
36
37 function convert(currency1, currency2, value) {
38 const host = "api.frankfurter.app";
39
40 fetch(`https://${host}/latest?amount=${value}&from=${currency1}&to=${currency2}`)
41 .then((val) => val.json())
42 .then((val) => {
43 console.log(Object.values(val.rates)[0]);
44 output.value = Object.values(val.rates)[0];
45 });
46
47 }