-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (137 loc) · 3.72 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
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
143
144
145
146
const searchInput = document.getElementById("searchInput")
let coins = []
const getData = async () => {
try {
const response = await fetch(`https://api.coinranking.com/v2/coins`);
if(response.ok){
const data = await response.json();
coins = data.data.coins
createTable(data.data.coins);
} else {
document.getElementById("list").innerHTML = `
<div class="text-center ">
<h1 class="display-1">404 <h1>
<h2 class="display-1">Not Found! </h2>
</div>
`
}
} catch (error) {
console.log("Data extraction failed:", error);
}
};
getData()
const createTable = (data) => {
const table = document.getElementById("coinTable");
let tableHTML = `
<thead>
<tr>
<th>#</th>
<th>Names</th>
<th>Price</th>
<th>Market Cap</th>
<th>Change <i class="fa-solid fa-arrow-up-right-dots"/></th>
</tr>
</thead>
<tbody class="table-group-divider">
`;
data.forEach((item) => {
tableHTML += `
<tr>
<td>${item.rank}</td>
<td>
<span><img src="${item.iconUrl}" width="25px" class="me-3"/>${item.name}</span>
<sup style="color:${item.color}">${
item.symbol
}</sup></td>
<td> $${Number(item.price).toFixed(4)}</td>
<td>${formatMarketCap(item.marketCap)}</td>
<td class="stonks">${item.change}</td>
</tr>
`;
});
tableHTML += `</tbody>`;
table.innerHTML = tableHTML;
// change cells
const changeCells = table.querySelectorAll(".stonks");
data.forEach((item, index) => {
const changeCell = changeCells[index];
const icon = changePrice(item.change);
changeCell.appendChild(icon);
if (item.change > 0) {
changeCell.style.color = "lightgreen";
}
else {
changeCell.style.color = "red";
}
});
};
// format dollar
const formatMarketCap = (marketCap) => {
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
return formatter.format(marketCap);
};
//color style
const changePrice = (change) => {
const stonk = document.createElement("i");
if (change > 0) {
stonk.className = "fa-solid fa-arrow-trend-up ms-3";
stonk.style.color = "lightgreen";
} else {
stonk.className = "fa-solid fa-arrow-trend-down ms-3";
stonk.style.color = "red";
}
return stonk;
};
searchInput.addEventListener("input", (e) => {
const table = document.getElementById("coinTable");
table.innerHTML = "";
let result = coins.filter((item) => item.name.toLowerCase().includes(e.target.value.toLowerCase()))
let tableHTML = `
<thead>
<tr>
<th>#</th>
<th>Names</th>
<th>Price</th>
<th>Market Cap</th>
<th>Change <i class="fa-solid fa-arrow-up-right-dots"/></th>
</tr>
</thead>
<tbody class="table-group-divider">
`;
result.forEach((item) => {
tableHTML += `
<tr>
<td>${item.rank}</td>
<td>
<span ><img src="${item.iconUrl}" class="me-3" width="25px" />${item.name}</span>
<sup style="color:${item.color}" ">${
item.symbol
}</sup></td>
<td> $${Number(item.price).toFixed(4)}</td>
<td>${formatMarketCap(item.marketCap)}</td>
<td class="stonks">${item.change}</td>
</tr>
`;
});
tableHTML += `</tbody>`;
table.innerHTML = tableHTML;
// change cells
const cells = table.querySelectorAll(".stonks");
const changeCellArray = Array.from(cells);
result.forEach((item, index) => {
const cell = changeCellArray[index];
const icon = changePrice(item.change);
cell.appendChild(icon);
if (item.change > 0) {
cell.style.color = "lightgreen";
}
else {
cell.style.color = "red";
}
});
});