Changed around line 1
+ // Sample data - in a real app, this would come from a backend
+ let players = [
+ { id: 1, name: "Alex Chen", score: 2500, change: 12 },
+ { id: 2, name: "Maria Garcia", score: 2450, change: -5 },
+ { id: 3, name: "James Wilson", score: 2400, change: 8 },
+ { id: 4, name: "Sarah Kim", score: 2350, change: 0 },
+ { id: 5, name: "David Lee", score: 2300, change: 15 },
+ // Add more players as needed
+ ];
+
+ // DOM Elements
+ const leaderboardBody = document.getElementById('leaderboardBody');
+ const searchInput = document.getElementById('searchInput');
+ const sortSelect = document.getElementById('sortSelect');
+ const themeToggle = document.getElementById('toggleTheme');
+
+ // Theme handling
+ function toggleTheme() {
+ document.body.dataset.theme = document.body.dataset.theme === 'dark' ? 'light' : 'dark';
+ localStorage.setItem('theme', document.body.dataset.theme);
+ }
+
+ // Initialize theme
+ document.body.dataset.theme = localStorage.getItem('theme') || 'light';
+ themeToggle.addEventListener('click', toggleTheme);
+
+ // Render leaderboard
+ function renderLeaderboard(players) {
+ leaderboardBody.innerHTML = '';
+
+ players.forEach((player, index) => {
+ const row = document.createElement('div');
+ row.className = 'player-row';
+
+ const changeClass = player.change > 0 ? 'positive' : player.change < 0 ? 'negative' : '';
+ const changeSymbol = player.change > 0 ? '↑' : player.change < 0 ? '↓' : '–';
+
+ row.innerHTML = `
+ #${index + 1}
+ ${player.name}
+ ${player.score}
+ ${changeSymbol} ${Math.abs(player.change)}
+ `;
+
+ leaderboardBody.appendChild(row);
+ });
+ }
+
+ // Search functionality
+ searchInput.addEventListener('input', (e) => {
+ const searchTerm = e.target.value.toLowerCase();
+ const filtered = players.filter(player =>
+ player.name.toLowerCase().includes(searchTerm)
+ );
+ renderLeaderboard(filtered);
+ });
+
+ // Sort functionality
+ sortSelect.addEventListener('change', (e) => {
+ const sortBy = e.target.value;
+ const sorted = [...players].sort((a, b) => {
+ if (sortBy === 'rank') return b.score - a.score;
+ if (sortBy === 'score') return b.score - a.score;
+ if (sortBy === 'name') return a.name.localeCompare(b.name);
+ return 0;
+ });
+ renderLeaderboard(sorted);
+ });
+
+ // Initial render
+ renderLeaderboard(players);