-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin.ts
49 lines (35 loc) · 1.05 KB
/
coin.ts
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
declare var $: any;
class Coin {
id: number
name: string
symbol: string
price_thb: number
constructor(coin: { id: number, name: string, symbol: string, price_thb: number }) {
this.id = coin.id
this.name = coin.name
this.symbol = coin.symbol
this.price_thb = coin.price_thb
}
print(): string {
return ` <div class="well well-lg">
<strong><h1><a href="https://coinmarketcap.com/currencies/${this.id}/">${this.name}</a></h1></strong> <h4> ชื่อย่อ : ${this.symbol} ราคา : ${this.price_thb} THB</h4>
</div>`
}
}
function getCoins() {
return fetch("https://api.coinmarketcap.com/v1/ticker/?convert=THB&limit=20").then((resp) => {
return resp.json()
})
}
function addCoin(coin: Coin): void {
const $coins = $('#coins')
$coins.append(`${coin.print()}`)
}
function displayCoins(coins: Coin[]): void {
coins.forEach(function (coin) {
addCoin(new Coin(coin))
})
}
getCoins().then((coins) => {
displayCoins((coins))
})