I am making a currency exchange app. Its very simple. However, i have been struggling to implement and come up with coding logic to convert from one currency to another.In the convertCurrency is where i am trying to make the code function. I did alot of erasing and rewriting. What you see in convertCurrency does not work. I am running out of ideas. Any help, ideas?
function exchangeRate() { fetch(`https://v6.exchangerate-api.com/v6/${apiKey}/latest/USD`) .then(response => { return response.json() }).then(data => { console.log(data) let jsonData = data.conversion_rates; const newArr = Object.keys(jsonData); appendData(newArr) convertCurrency(newArr, jsonData, data) }).catch(err => { console.log(err) }) const appendData = (newArr) => { //console.log(newArr); let output = '' for (let i = 0; i < newArr.length; i++) { output = `<option>${newArr[i]}</option>`; fromCurrency.innerHTML += output toCurrency.innerHTML += output } } //get fromCurrency and toCurrency const convertCurrency= (newArr, jsonData, data) => { let input = userInput.value; console.log(input); let monetaryVal = jsonData console.log(monetaryVal); for (const key in monetaryVal) { //console.log(`${key}: ${monetaryVal[key]}`) let fromCurrency = monetaryVal[key] * input; console.log(fromCurrency) let toCurrency = monetaryVal[key]; } } convertCurrency() convertBtn.addEventListener('click', e => { convertCurrency(); e.preventDefault() }) } exchangeRate();
https://stackoverflow.com/questions/67238965/algorithm-to-convert-one-currency-to-another-using-api April 24, 2021 at 11:03AM
没有评论:
发表评论