-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (49 loc) · 1.44 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
const COIN_RANKING_API_URL = "https://pure-woodland-29790.herokuapp.com/coins"
const coinList = document.getElementById("data")
const filterInput = document.getElementById("filter")
let coinsData = []
let filteredCoins = []
var formatCash = n => {
if (n < 1e3) return n;
if (n >= 1e4) return + (n / 1e9).toFixed(2) + "billion ETC"
}
filterInput.addEventListener('keyup', (e) => {
const inputValue = e.target.value
filteredCoins = coinsData.filter(coin => {
return coin.name.toLowerCase().includes(inputValue)
})
displayCoins(filteredCoins)
})
const loadCoins = async () => {
try {
const res = await fetch(COIN_RANKING_API_URL)
const dataResponse = await res.json()
coinsData = dataResponse.data.coins
console.log(dataResponse)
displayCoins(dataResponse.data.coins)
} catch (error) {
console.log(error)
}
}
const displayCoins = (coins) => {
const htmlString = coins.map((coin) => {
return `
<tr>
<td>${coin.name}</td>
<td>${coin.rank}</td>
<td>${new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(coin.price)}</td>
<td>${formatCash(coin.marketCap)}</td>
<td>${coin.symbol}</td>
<td><img src="${coin.iconUrl}" height="25" width="25" /></td>
<td>
<a href="${coin.coinrankingUrl}" target="_blank">
<i class="fas fa-chart-line"></i>
</a>
</td>
</tr>
`
})
.join('');
coinList.innerHTML = htmlString
}
loadCoins()