-2
The problem is that by clicking the convert button it consumes the API E shows the correct quote in the debug and console.
But in time to pass the value moedaBVal = cotaçao * moedaAVal
he passes as moedaBVal = undefined
or NaN
.
What could be the problem here?
Follows the code:
class Converter {
converter() {
let moedaA = "USD";
let moedaB = "BRL";
/* let moedaBVal = 0; */
let moedaAVal = document.getElementById("inputUSDBRL").value;
let de_para = moedaA + "_" + moedaB;
let url = `https://free.currconv.com/api/v7/convert?q=${de_para}&compact=ultra&apiKey=234428564665e4c14ce2`;
fetch(url).then(res=>{return res.json()}).then(json=>{
let cotacao = json;
let moedaBVal = cotacao * moedaAVal;
});
}
}
class Controller {
constructor() {
this.converter = new Converter();
}
aoClicarConverter() {
let converter = this.converter.converter();
let div = `<h5>${converter}</h5>`;
document.getElementById("resultadoUSDBRL").innerHTML = div;
}
domInputDolarEmReais() {
let moedaA = "USD"
let moedaB = "BRL"
let div = document.getElementById("titlleConversorUSDBRL")
div.innerHTML = moedaA + " Para " + moedaB
}
}
let controller = new Controller;
<body onload="controller.domInputDolarEmReais()">
<h5 id="titlleConversorUSDBRL"></h5>
<input type="number" placeholder="USD" id="inputUSDBRL">
<button onclick="controller.aoClicarConverter()">Converter</button>
<div id="resultadoUSDBRL"></div>
</body>
You will need to handle the api call. In function. With async, and promisse. I will see if I can get example and post.
– Rebeca Nonato
If possible, change the question title to something more descriptive, remember when you Google search and find titles that are "exactly what you need". Try to provide the same feeling when someone finds your question too :)
– BrunnoFdc