-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (87 loc) · 3.35 KB
/
app.js
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
97
98
99
100
101
//References to elements on the page
const cashAmt = document.querySelector('.cash')
const form1 = document.querySelector('.form1')
const currencySelection = document.querySelector('.currencySelect')
const currentRate = document.querySelector('.currentRate')
const calcBtn = document.querySelector('.calcBtn')
const clrBtn = document.querySelector('.clrBtn')
const total = document.querySelector('.convertedValue')
const errMsg = document.querySelector('.errMsg')
const regEx = /^\d+(\.\d*)?$|^\.\d+$/
//Display current date on page
let datePara = document.querySelector('.datePara')
datePara.textContent = new Date().toDateString()
//Function to perform calculation based on the input selected
//'Change" is used as the event trigger, it tracks the change in the selection group
currencySelection.addEventListener('change', () => {
if (currencySelection.value === 'usd') {
currencies().then(data => {
calcUsd = data.conversion_rates.JMD
currentRate.textContent = `\$${calcUsd.toFixed(2)}`
currRateCalcValue = calcUsd.toFixed(2)
});
}
else if (currencySelection.value === 'gbp') {
currencies().then(data => {
calcGbp = (1 / (data.conversion_rates.GBP)) * data.conversion_rates.JMD
currentRate.textContent = `\$${calcGbp.toFixed(2)}`
currRateCalcValue = calcGbp.toFixed(2)
})
}
else if (currencySelection.value === 'cad') {
currencies().then(data => {
calcCad = (1 / (data.conversion_rates.CAD)) * data.conversion_rates.JMD
currentRate.textContent = `\$${calcCad.toFixed(2)}`// the /escapes the dollar sign
currRateCalcValue = calcCad.toFixed(2)
})
}
else if (currencySelection.value === 'eur') {
currencies().then(data => {
calcEur = (1 / (data.conversion_rates.EUR)) * data.conversion_rates.JMD
currentRate.textContent = `\$${calcEur.toFixed(2)}`
currRateCalcValue = calcEur.toFixed(2)
})
}
else if (currencySelection.value === 'kyd') {
currencies().then(data => {
calcKyd = (1 / (data.conversion_rates.KYD)) * data.conversion_rates.JMD //curr xchange rate
currentRate.textContent = `\$${calcKyd.toFixed(2)}` //value displayed as rate
currRateCalcValue = calcKyd.toFixed(2) //value used to calculate
})
}
});
//API FUNCTION
const currencies = async () => {
const response = await fetch(' https://v6.exchangerate-api.com/v6/e0a0d5980401e211ef627e93/latest/USD')
const data = await response.json()
return data
}
//Function to validate the cash input
cashAmt.addEventListener('keyup', e => {
if (regEx.test(e.target.value)) {
true;
errMsg.style.visibility = "hidden"
} else {
errMsg.style.visibility = "visible"
cashAmt.value = ""
total.value = ""
}
})
//function to check for empty values
function checkEmptyVals() {
if (cashAmt.value === '') {
alert('There are empty values')
total.textContent = ''
currentRate.innerHTML = ''
}
}
//Perform Calculation
calcBtn.addEventListener('click', () => {
calcValue = (Number(currRateCalcValue * cashAmt.value).toFixed(2))
total.textContent = `$${calcValue}`
checkEmptyVals()
})
//clear form
clrBtn.addEventListener('click', () => {
window.location.reload()
})