-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (92 loc) · 3.15 KB
/
script.js
File metadata and controls
107 lines (92 loc) · 3.15 KB
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
// Function to fetch cryptocurrency data from CoinRanking API
async function fetchCryptoData() {
try {
const response = await
fetch('https://api.coinranking.com/v2/coins');
const data = await response.json();
return data.data.coins;
} catch (error) {
console.error('Error fetching cryptocurrency data:', error);
return [];
}
}
// Function to display cryptocurrency data in the table
function displayCryptoData(coins) {
const cryptoTable = document.getElementById('cryptoTable');
cryptoTable.innerHTML = '';
coins.forEach(coin => {
const row = document.createElement('tr');
row.innerHTML = `
<td><img src="${coin.iconUrl}"
class="crypto-logo" alt="${coin.name}"></td>
<td>${coin.name}</td>
<td>${coin.symbol}</td>
<td>$${coin.price}</td>
<td>${coin.change}%</td>
<td>${coin.volume ? coin.volume : '-- N/A --'}</td>
<td>${coin.marketCap ? coin.marketCap : '-'}</td>
`;
cryptoTable.appendChild(row);
});
}
// Function to filter cryptocurrencies based on user input
function filterCryptoData(coins, searchTerm) {
searchTerm = searchTerm.toLowerCase();
const filteredCoins = coins.filter(coin =>
coin.name.toLowerCase().includes(searchTerm) ||
coin.symbol.toLowerCase().includes(searchTerm)
);
return filteredCoins;
}
// Function to handle search input
function handleSearchInput() {
const searchInput = document.getElementById('searchInput');
const searchTerm = searchInput.value.trim();
fetchCryptoData().then(coins => {
const filteredCoins = filterCryptoData(coins,
searchTerm);
displayCryptoData(filteredCoins);
});
}
// Function to initialize the app
async function initializeApp() {
const coins = await fetchCryptoData();
displayCryptoData(coins);
// Add event listener to search input
const searchInput =
document.getElementById('searchInput');
searchInput.addEventListener('input',
handleSearchInput);
copyrightYear();
separator();
license();
separator2();
developer();
}
// Call initializeApp function
// when the DOM content is loaded
document.addEventListener('DOMContentLoaded'
, initializeApp);
function copyrightYear() {
var year = new Date().getFullYear();
const repositoryURL = "https://github.com/devsujay19/CryptoTracker";
const repositoryName = "CryptoTracker";
document.getElementById("appName").innerText = repositoryName;
document.getElementById("copyrightYear").innerHTML = `© ${year} | <a href=${repositoryURL}>${repositoryName}<a>`;
};
function separator() {
document.getElementById("separator").innerHTML = `·`;
};
function license() {
const licenseFileURL = "https://github.com/devsujay19/CryptoTracker/blob/main/LICENSE";
const license = "MIT";
document.getElementById("license").innerHTML = `Released under <a href="${licenseFileURL}">${license}</a>`;
};
function separator2() {
document.getElementById("separator2").innerHTML = `·`;
};
function developer() {
const developerURL = "https://github.com/devsujay19";
const developerName = "Sujay Mukherjee";
document.getElementById("developer").innerHTML = `<a href="${developerURL}">${developerName}</a>`;
};