-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoQuote.js
96 lines (94 loc) · 3.25 KB
/
cryptoQuote.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
const inputField = document.querySelector(".js-cryptoInputField");
const form = document.querySelector(".js-form");
const cryptoInputField = document.querySelector(".js-cryptoInputField");
const listAllBtn = document.querySelector(".js-listAll");
const searchBtn = document.querySelector(".js-search");
const theader = document.querySelector(".thead-visibility");
var listAllClicked = false;
var html = "";
var searchExpression = "";
var arrayIndex = "";
let dollarUSLocale = Intl.NumberFormat('en-US', {
maximumFractionDigits: 20,
style: "currency",
currency: "USD",
});
form.addEventListener("submit", cryptoQuote);
function cryptoQuote(event) {
event.preventDefault();
theader.style.visibility="hidden";
listAllClicked = false;
document.querySelector(".js-quote-tbody").style.fontSize = "16px";
searchExpression = inputField.value;
inputField.value = "";
let searchExpressionLowerCase = searchExpression.toLowerCase();
searchExpression = searchExpressionLowerCase.trim();
if (searchExpression.length === 0) {
listAllClicked = true;
};
let API_URL = `https://api.coingecko.com/api/v3/coins/`;
fetch(API_URL)
.then((data) => data.json())
.then((responses) => {
function findCryptoIndex(response) {
for (i = 0; i < response.length; i+=1 ) {
if (response[i].name.toLowerCase() === searchExpression) {
return i;
}
}
return -1;
}
arrayIndex = findCryptoIndex(responses);
if (arrayIndex === -1 && searchExpression.length > 0) {
document.querySelector(".js-quote-tbody").style.fontSize = "18px";
document.querySelector(".js-quote-tbody").style.fontWeight = "bold";
document.querySelector(".js-quote-tbody").innerHTML = "You're selection is not in our database. Please try again.";
return false;
}
buildQuoteTable(responses);
}
);
}
function buildQuoteTable(response) {
const finalCryptoArray = [];
if (listAllClicked === false) {
finalCryptoArray.push(response[arrayIndex]);
} else {
Array.prototype.push.apply(finalCryptoArray, response);
let sortedArray = finalCryptoArray.sort(cryptoNameCompare);
}
var header = "";
var row = "";
html = "";
theader.style.visibility="visible";
document.querySelector(".js-quote-tbody").innerHTML="";
let mvgAvg200Day = ""
for (let i = 0; i < finalCryptoArray.length; i++) {
if (isNaN(finalCryptoArray[i].market_data.price_change_percentage_200d_in_currency.usd)) {
mvgAvg200Day = "Note enough data"}
else{
mvgAvg200Day = finalCryptoArray[i].market_data.price_change_percentage_200d
}
try{
row = `<tr>
<td>${finalCryptoArray[i].name}</td>
<td>${finalCryptoArray[i].symbol}</td>
<td>${dollarUSLocale.format(finalCryptoArray[i].market_data.current_price.usd)}</td>
<td>${mvgAvg200Day}</td>
</tr>`;
html = html += row;
}
catch{
theader.style.visibility="hidden";
html = "Your crypto selection is not in our database";
}
}
function cryptoNameCompare (response1, response2) {
if (response1.name.toUpperCase() > response2.name.toUpperCase()) {
return 1;
} else {
return -1;
}
}
document.querySelector(".js-quote-tbody").innerHTML = html;
}