-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
96 lines (76 loc) · 2.71 KB
/
Copy pathscripts.js
File metadata and controls
96 lines (76 loc) · 2.71 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const iconEnglish = document.getElementById('icon_englosh');
const iconPortuguese = document.getElementById('icon_portuguese');
const text1 = document.getElementById('text_1');
const text2 = document.getElementById('text_2');
const data = document.getElementById('data');
const inputDolar = document.getElementById('input_dolar');
const inputReal = document.getElementById('input_real');
const dateNow = new Date();
const monthNowBR = dateNow.toLocaleString('pt-BR', { month: 'long' });
const monthNowUS = dateNow.toLocaleString('en-US', { month: 'long' });
const dayNow = dateNow.getDate();
const monthDayBR = `${dayNow} de ${monthNowBR.charAt(0).toUpperCase() + monthNowBR.slice(1)}`;
const monthDayUS = `${monthNowUS.charAt(0).toUpperCase() + monthNowUS.slice(1)} ${dayNow}`;
let dolarbrr = null
async function getDolarValue(){
try{
const res = await fetch('https://economia.awesomeapi.com.br/json/last/USD-BRL');
const data = await res.json();
const dolarBrValueNow = parseFloat(data.USDBRL.bid)
dolarbrr = parseFloat(data.USDBRL.bid)
return dolarBrValueNow;
} catch (err){
console.log("Error to search reais: ", err);
return null;
}
}
getDolarValue().then(value =>{
if(value != null){
const dolarBr = value.toFixed(2);
messages.portuguese.text2 = `${dolarBr} Reais Brasileiros`;
messages.english.text2 = `${dolarBr} Brazilian Real`;
text2.textContent = messages[currentLanguage].text2;
inputReal.value = dolarBr;
};
});
let messages = {
english: {
text1: "1 United States Dollar =",
text2: "",
data: monthDayUS
},
portuguese: {
text1: "1 Dólar Estadunidense =",
text2: "",
data: monthDayBR
}
};
let currentLanguage = "portuguese";
window.addEventListener('DOMContentLoaded', () => {
text1.textContent = messages['portuguese'].text1;
text2.textContent = messages['portuguese'].text2;
data.textContent = messages['portuguese'].data;
});
function languageChange(language){
currentLanguage = language;
text1.textContent = messages[currentLanguage].text1;
text2.textContent = messages[currentLanguage].text2;
data.textContent = messages[currentLanguage].data;
};
inputDolar.value = 1;
inputDolar.addEventListener('input', () => {
const dolarValue = inputDolar.value;
if (dolarValue != '') {
inputReal.value = (dolarValue * dolarbrr).toFixed(2);
} else {
inputReal.value = '';
}
});
inputReal.addEventListener('input', () => {
const realValue = inputReal.value;
if (inputReal.value === ''){
inputDolar.value = ''
} else {
inputDolar.value = (realValue / dolarbrr).toFixed(2);
}
});