-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.js
30 lines (30 loc) · 970 Bytes
/
currency.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
class Currency{
constructor(firstCurrency,secondCurrency){
this.firstCurrency = firstCurrency;
this.secondCurrency = secondCurrency;
this.url = "https://api.exchangeratesapi.io/latest?base=";
this.amount = null;
}
exchange(){
return new Promise((resolve,reject) => {
fetch(this.url + this.firstCurrency)
.then(res => res.json())
.then(data => {
const sec = data["rates"][this.secondCurrency];
const tempAmount = Number(this.amount);
const total = tempAmount * sec;
resolve(total);
})
.catch(err => reject(err))
});
}
changeAmount(newAmount){
this.amount = newAmount;
}
changeFirstCurrency(newFirstCurrency){
this.firstCurrency = newFirstCurrency;
}
changeSecondCurrency(newSecondCurrency){
this.secondCurrency = newSecondCurrency;
}
}