// Function to fetch the username async function fetchUsername() { const userId = localStorage.getItem("userId"); // Replace with the actual user ID const secret = localStorage.getItem("secret") try { const response = await fetch(`/api/user/test/${userId}/${secret}`); const userData = await response.json(); // Call function to display username displayUsername(localStorage.getItem("username")); displayTestResults(userData); } catch (error) { console.error('Error fetching username:', error); } } // Function to display test results in a table function displayTestResults(testData) { const testResultsDiv = document.getElementById('testResults'); if (!testData) { testResultsDiv.innerHTML = '

No test data available.

'; return; } // Construct HTML for the table const html = `
Test Type Test Length Test Time Test Seed Quote ID Words Per Minute (WPM) Accuracy
${testData.test_type} ${testData.test_length} ${testData.test_time} ${testData.test_seed} ${testData.quote_id} ${testData.wpm} ${testData.accuracy}
`; // Update the testResultsDiv with the HTML testResultsDiv.innerHTML = html; } // Function to display username function displayUsername(username) { const usernameDiv = document.getElementById('username'); if (!username) { usernameDiv.innerHTML = '

No username available.

'; return; } // Update the usernameDiv with the username usernameDiv.innerHTML = `

Welcome, ${username}!

`; } // Call the fetchUsername function when the page loads window.onload = fetchUsername;