-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
142 lines (121 loc) · 5.14 KB
/
Copy pathjavascript.js
File metadata and controls
142 lines (121 loc) · 5.14 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//////////////////// CURRENT TIME CODE
// get the current time using moment.js
// and using setInterval to update the time lively
function setCurrentTime() {
var localTime = moment.utc().toDate();
var currentTime = moment(localTime).format("HH:mm:ss MM/DD/YY");
$(".current-time").text(currentTime);
}
setCurrentTime();
var clockUpdate = setInterval( function() {
setCurrentTime();
}, 1000)
setInterval(clockUpdate, 1000);
//////////////////// CURRENCY EXCHANGE CODE
// gets local time using moment.js library
localTime = moment.utc().toDate();
// format the time
var currentTime = moment(localTime).format("YYYY-MM-DD");
// get yesterday's time using moment.js
var yesterday = moment(currentTime).add(-1, 'days').format("YYYY-MM-DD");
console.log(yesterday);
var currency = $(this).attr("data-name");
// the query url with api key used to make an AJAX call
var moneyUrl = "https://openexchangerates.org/api/latest.json?app_id=072740dd2d5c4bc996fd2fcfcbacbd1d";
// query url that takes the day as query paramater to give us past currency exchange rates
var moneyHistoryUrl = "https://openexchangerates.org/api/historical/" + yesterday + ".json?app_id=072740dd2d5c4bc996fd2fcfcbacbd1d";
// make an AJAX call to get the exchange rates
$.ajax({
url: moneyUrl,
method: "GET"
}).then(function (response) {
// console log the response
console.log(response);
// get the base currency
var baseCurr = response.base;
// get the exchange rate in euros
var euroRate = response.rates.EUR;
euroRate = euroRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#euroRate").html("€" + euroRate);
// get exchange rate for yen
var jpyRate = response.rates.JPY;
jpyRate = jpyRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#jpyRate").html("¥" + jpyRate);
// get the exchange rate for great british pound
var gbpRate = response.rates.GBP;
gbpRate = gbpRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#gbpRate").html("£" + gbpRate);
// get the exchange rate for canadian dollar
var cadRate = response.rates.CAD;
cadRate = cadRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#cadRate").html("$" + cadRate);
// get the exchange rate for kuwaiti dinar
var kwdRate = response.rates.KWD;
kwdRate = kwdRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#kwdRate").html("¤" + kwdRate);
// get the exchange rate for indian rupee
var inrRate = response.rates.INR;
inrRate = inrRate.toFixed(2);
// passes the value stored inside the variable into an id
$("#inrRate").html("₹" + inrRate);
// make an ajax call to get yesterday's rates and compare them with current rates
$.ajax({
url: moneyHistoryUrl,
method: "GET"
}).then(function (response) {
// checked if the current rate is greater than yesterday
if (inrRate < response.rates.INR) {
$("#inrRate").attr("style", "color:red;");
$("#inrArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#inrRate").attr("style", "color:green;");
$("#inrArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
if (jpyRate < response.rates.JPY) {
$("#jpyRate").attr("style", "color:red;");
$("#jpyArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#jpyRate").attr("style", "color:green;");
$("#jpyArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
if (cadRate < response.rates.CAD) {
$("#cadRate").attr("style", "color:red;");
$("#cadArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#cadRate").attr("style", "color:green;");
$("#cadArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
if (gbpRate < response.rates.GBP) {
$("#gbpRate").attr("style", "color:red;");
$("#gbpArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#gbpRate").attr("style", "color:green;");
$("#gbpArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
if (kwdRate < response.rates.KWD) {
$("#kwdRate").attr("style", "color:red;");
$("#kwdArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#kwdRate").attr("style", "color:green;");
$("#kwdArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
if (euroRate < response.rates.EUR) {
$("#euroRate").attr("style", "color:red;");
$("#eurArrow").html("<img src='assets/images/red-down.svg' width='10'>");
}
else {
$("#euroRate").attr("style", "color:green;");
$("#eurArrow").html("<img src='assets/images/green-up.svg' width='10'>");
}
})
});