-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
69 lines (64 loc) · 3.41 KB
/
main.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
document.addEventListener('DOMContentLoaded', () => {
// fetch request to coinbase api to retrieve BTC price data
const button = document.querySelector('.search-btn')
button.addEventListener('click', () => {
const input = document.getElementById("coin").value;
console.log("The term searched for was " + input);
// fetch price data from coinbase api
fetch('https://api.coinbase.com/v2/prices/' + input + '-USD/buy')
.then((data) => data.json())
.then((data) => {
console.log(data)
const removeImg = document.querySelector('img');
if (removeImg) removeImg.remove();
// add logic here to determine which icon to show
if (input.toLowerCase() !== 'doge' && input.toLowerCase() !== 'btc' && input.toLowerCase() !== 'eth') imageLink = 'images/elon.jpg'
else imageLink = 'images/' + input.toLowerCase() + '.jpg'
// imageLinkDefault = 'images/elon.jpg'
// if imageLink doesn't exist, DOM manipulate img with alternate image
const image = document.createElement('img');
image.setAttribute('src', imageLink)
// if (checkFileExist(imageLink)) image.setAttribute('src', imageLink);
// else image.setAttribute('src', imageLinkDefault);
document.querySelector('.price-display').appendChild(image)
const removeThis = document.querySelector('.price');
if (removeThis) removeThis.remove();
const btcPrice = `$${data.data.amount} ${data.data.currency}`
const price = document.createElement('div');
price.setAttribute('class', 'price')
price.innerText = `${data.data.base} Price : ` + btcPrice;
document.querySelector('.price-display').appendChild(price);
}).catch((error) => {
console.log(error)
const removeThis = document.querySelector('.price');
if (removeThis) removeThis.remove();
const removeImg = document.querySelector('img');
if (removeImg) removeImg.remove();
const price = document.createElement('div');
price.innerText = "Cryptocurrency not found";
price.setAttribute('class', 'price')
document.querySelector('.price-display').appendChild(price);
})
})
})
// const coin = document.getElementById("coin").value
// document.addEventListener('DOMContentLoaded', () => {
// fetch('https://api.coinbase.com/v2/prices/' + coin + '-USD/buy')
// .then((data) => data.json())
// .then((data) => {
// console.log(data)
// const btcPrice = `$${data.data.amount} ${data.data.currency}`
// const price = document.createElement('div');
// price.innerText = " Price : " + btcPrice;
// price.setAttribute('class', 'price')
// document.querySelector('body').appendChild(price);
// }).catch((error) => {
// console.log(error)
// const price = document.createElement('div');
// price.innerText = "Cryptocurrency not found";
// document.querySelector('body').appendChild(price);
// })
// })
// {data: {…}}
// data: {base: 'BTC', currency: 'USD', amount: '43034.19'}
// [[Prototype]]: Object