commit 6379c6154ba1308618d186ba999a82a9cfa0e97b138d5c9b42d666ded4d801b2 Author: William Cardoso Simas Date: Wed Nov 18 09:48:55 2026 -0300 first commit diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..7aa0f27 --- /dev/null +++ b/Readme.md @@ -0,0 +1,27 @@ +## Conversor de moedas + +Projeto desenvolvido na imersão JavaScript da Alura, realizada entre os dias 13/11/2023 e 17/11/2023. Trata-se de um conversor de moedas desenvolvido no primeiro dia da imersão (13/11/2023). + +# Conteúdo da aula +Durante a aula, o foco foi o uso do javascript como algoritmo, com saídas via alert(). + +# Desafios +Os desafios foram atividades deixadas ao fim da aula para serem resolvidas. + + + +# Melhorias +As melhorias são itens incluídos no projeto e que não foram solicitados nas aulas ou desafios. + + \ No newline at end of file diff --git a/calc.js b/calc.js new file mode 100644 index 0000000..3c34257 --- /dev/null +++ b/calc.js @@ -0,0 +1,118 @@ +//carrega dados de uma página externa +async function carregarPaginaExterna(url) { + var prom = new Promise(function (resolve, reject) { + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function () { + if (xhr.readyState == 4 && xhr.status == 200) { + resolve(xhr.responseText); + } + }; + + xhr.open("GET", url, true); + xhr.send(); + }); + + return await prom; +} + +//converte os valores +document.getElementById("form").addEventListener('submit', (e) => { + converterValores(); + e.preventDefault(); +}); + +async function converterValores() { + var retorno = ""; + + var moedaSelecionada = document.getElementById("moeda"); + var nome = document.getElementById("nome").value; + var valor = document.getElementById("valor").value; + + if (nome == "") { + retorno = "Informe seu nome"; + } + else if (valor == "") { + retorno = "Informe um valor"; + } + else if (isNaN(Number(valor))) { + retorno = "Informe um número"; + } + else { + var chave = moedaSelecionada.value; + var auxChave = chave.split("-"); + var cotacao = await obterCotacao(chave); + var cotacaoUSD = await obterCotacao(auxChave[0] + "-USD"); + var cotacaoUSDBTC = await obterCotacao("BTC-USD"); + + + if (cotacao != null && cotacaoUSD != null && cotacaoUSDBTC != null) { + retorno = "Olá " + nome + '
' + retorno += Number(valor).toFixed(2) + " " + auxChave[0] + " = " + (valor * cotacao).toFixed(2) + " " + auxChave[1] + "
"; + retorno += Number(valor).toFixed(2) + " " + auxChave[0] + " = " + (valor * cotacaoUSD / cotacaoUSDBTC).toFixed(7) + " BTC
"; + } + else { + retorno = "Não foi possível carregar os dados da moeda!"; + } + } + + document.getElementById("resultado").innerHTML = retorno; +} + +async function obterCotacao(moeda) { + var auxChave = moeda.split("-"); + + var respostaTexto = await carregarPaginaExterna("https://economia.awesomeapi.com.br/last/" + moeda); + var conversao = null; + if (respostaTexto != null) { + conversao = JSON.parse(respostaTexto); + conversao = conversao[auxChave[0] + auxChave[1]].ask; + } + + return conversao; +} + +//carrega as moedas +async function carregarMoedas() { + var respostaTexto = await carregarPaginaExterna("https://economia.awesomeapi.com.br/xml/available"); + + if (respostaTexto != null) { + //Limpamdo texto + var aux = respostaTexto.search(''); + respostaTexto = respostaTexto.substring(aux + 5, respostaTexto.length); + + aux = respostaTexto.search(''); + respostaTexto = respostaTexto.substring(0, aux); + + // Carregando moedas + var tags = respostaTexto.split(">"); + var moedas = []; + for (var i = 0; i < tags.length - 2; i += 2) { + var indice = tags[i].substring(1, tags[i].length); + var auxNome = tags[i + 1].split("<"); + var nome = auxNome[0]; + moedas[indice] = nome; + } + + var objMoedas = Object.entries(moedas); + + objMoedas.sort(function (a, b) { + var keyA = a[0]; + var keyB = b[0]; + + return keyA.localeCompare(keyB); + }); + + //Preenchendo valores + var opcoes = ""; + for (var i = 0; i < objMoedas.length; i++) { + var indice = objMoedas[i][0]; + var nome = objMoedas[i][1]; + var auxNomeSplit = nome.split("/"); + var auxIndice = indice.split("-"); + var selecionado = (indice == "BRL-USD") ? "selected" : ""; + opcoes += ''; + } + document.getElementById("moeda").innerHTML = opcoes; + } +} \ No newline at end of file diff --git a/img/Background.png b/img/Background.png new file mode 100644 index 0000000..941ac75 Binary files /dev/null and b/img/Background.png differ diff --git a/img/Conversao_1.png b/img/Conversao_1.png new file mode 100644 index 0000000..e917e1e Binary files /dev/null and b/img/Conversao_1.png differ diff --git a/img/alura-logo.svg b/img/alura-logo.svg new file mode 100644 index 0000000..1c6fb30 --- /dev/null +++ b/img/alura-logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/img/logo-imersao-aluraflix.svg b/img/logo-imersao-aluraflix.svg new file mode 100644 index 0000000..21821a0 --- /dev/null +++ b/img/logo-imersao-aluraflix.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/img/splash.png b/img/splash.png new file mode 100644 index 0000000..cf236b7 Binary files /dev/null and b/img/splash.png differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..2e6beea --- /dev/null +++ b/index.html @@ -0,0 +1,52 @@ + + + + + + + + + Conversor de moedas + + + +
+
+

+ Conversor de moedas +

+ +
+ + + + +
+ +
+
+ + + + + + + + + + +
+
+ +
+ + + + + + + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..6dbe4e6 --- /dev/null +++ b/style.css @@ -0,0 +1,110 @@ +*{ + font-family: "Roboto Mono", monospace; + font-size: 16px; +} + +body { + text-align: center; + background-image: url("img/Background.png"); + background-color: #000000; + background-size: cover; + background-position: center top; + background-repeat: no-repeat; + /*height: 100vh;*/ +} + +.container { + text-align: center; + padding: 20px; +} + +.page-title { + color: #ffffff; + margin: 0 0 5px; +} + +.page-subtitle { + color: #ffffff; + margin-top: 5px; +} + +.page-logo { + width: 200px; +} + +.alura-logo { + width: 40px; + position: absolute; + top: 10px; + right: 10px; +} + +body>img { + margin: 0 10px; +} + +img { + max-height: 250px; +} + +form{ + margin-left: auto; + margin-right: auto; + display: flex; + flex-direction: column; + max-width: 600px; + text-align: center; + +} + +form label{ + color:white; + margin-top: 10px; +} + +form input{ + text-align: center; +} + +select{ + text-align: center; +} + +#resultado +{ + color: white; + margin-top: 10px; +} + +button +{ + margin-top: 10px; + padding: 10px; + border: 0; + border-radius: 5px; + background-color: cyan; + border-color: darkcyan; + box-shadow: none; + font-size: 16px; +} + +.resultado +{ + flex-grow: 1; + color:white; + margin-bottom: 25px; +} +footer +{ + background-color: #000000; + color: #ffffff; + display: block; + position: fixed; + bottom: 0; + width: 101%; + left: -1%; +} + +a{ + color:#ffffff; +} \ No newline at end of file