Changed around line 1
+ const gdpData = [
+ { country: 'United States', gdp: 21433225 },
+ { country: 'China', gdp: 14342903 },
+ { country: 'Japan', gdp: 5081770 },
+ { country: 'Germany', gdp: 3845630 },
+ // Add more countries as needed
+ ];
+
+ document.addEventListener('DOMContentLoaded', () => {
+ const container = document.getElementById('gdpContainer');
+ const sortSelect = document.getElementById('sortOrder');
+ const toggleBtn = document.getElementById('toggleView');
+ const loading = document.getElementById('loading');
+
+ function formatGDP(gdp) {
+ return new Intl.NumberFormat('en-US', {
+ style: 'currency',
+ currency: 'USD',
+ maximumFractionDigits: 0,
+ notation: 'compact',
+ compactDisplay: 'short'
+ }).format(gdp * 1000000); // Convert to millions
+ }
+
+ function renderCountries(data) {
+ container.innerHTML = '';
+ data.forEach((item, index) => {
+ const card = document.createElement('div');
+ card.className = 'country-card';
+ card.innerHTML = `
+
${index + 1}. ${item.country}
+ `;
+ container.appendChild(card);
+ });
+ }
+
+ function sortData(order) {
+ const sortedData = [...gdpData].sort((a, b) => {
+ return order === 'asc' ? a.gdp - b.gdp : b.gdp - a.gdp;
+ });
+ renderCountries(sortedData);
+ }
+
+ // Event listeners
+ sortSelect.addEventListener('change', (e) => {
+ sortData(e.target.value);
+ });
+
+ toggleBtn.addEventListener('click', () => {
+ container.style.display = container.style.display === 'grid' ? 'block' : 'grid';
+ });
+
+ // Initial render
+ loading.style.display = 'none';
+ sortData('desc');
+ });
+
+ // Add smooth scrolling
+ document.querySelectorAll('a[href^="#"]').forEach(anchor => {
+ anchor.addEventListener('click', function (e) {
+ e.preventDefault();
+ document.querySelector(this.getAttribute('href')).scrollIntoView({
+ behavior: 'smooth'
+ });
+ });
+ });