-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (38 loc) · 1.27 KB
/
script.js
File metadata and controls
46 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const form = document.querySelector("form")
const input = document.querySelector("input")
const select = document.querySelector("select")
const description = document.querySelector("#description")
const result = document.querySelector("#result")
const footer = document.querySelector("footer")
form.addEventListener("submit", Identifica)
function Identifica(e){
e.preventDefault()
footer.style.display = "block"
let valor = 0
let moeda = ""
let resultado = 0
const inputValue = parseFloat(input.value.replace(",","."))
if (isNaN(inputValue) || inputValue <= 0) {
alert("Por favor, insira um valor válido.")
return
}
switch (select.value){
case "USD":
valor = 6.22
moeda = "US$"
break;
case "EUR":
valor = 6.41
moeda = "EUR€"
break;
case "GBP":
valor = 7.69
moeda = "GBP£"
}
resultado = inputValue * valor
substitui(valor, moeda, resultado)
}
function substitui(valor, moeda, resultado){
description.textContent = `${moeda} 1 - R$ ${valor.toString().replace(".", ",")} `
result.textContent = `${resultado.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'}).replace("R$", "")} Reais`
}