Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How do I log results for a leaderboard?

Edrol97

Silver Coder
I'm wanting to log results for a specific quiz game I've made for a leaderboard. How would I do this in js? The quiz game would be for a corporate workshop that I am eventually going to be running and I'm just trying to have a bit of fun at work by coding this. I've got a base of the game and quiz live at this link, but yeah. If this is possible it would be great. I would have to see the results of people who did the quiz whilst on the workshop in order to adjudicate, so yeah. How could I do this?
 
I'm wanting to log results for a specific quiz game I've made for a leaderboard. How would I do this in js? The quiz game would be for a corporate workshop that I am eventually going to be running and I'm just trying to have a bit of fun at work by coding this. I've got a base of the game and quiz live at this link, but yeah. If this is possible it would be great. I would have to see the results of people who did the quiz whilst on the workshop in order to adjudicate, so yeah. How could I do this?

Hi! Seems like a pretty cool idea. You can achieve this a couple ways. You can store the data locally, or have it save to a database and retrieve it from there. If you were to store locally, here is an example:

Save and retrieve quiz results:

JavaScript:
function saveResultToLocal(userName, score) {
    const results = JSON.parse(localStorage.getItem('quizResults')) || [];
    
    results.push({
        name: userName,
        score: score,
        timestamp: new Date().toISOString(),
    });

    // Sort by score (descending order)
    results.sort((a, b) => b.score - a.score);

    localStorage.setItem('quizResults', JSON.stringify(results));
    console.log('Results saved locally:', results);
}

function getLeaderboardFromLocal() {
    return JSON.parse(localStorage.getItem('quizResults')) || [];
}

Example:

Display leaderboard

JavaScript:
function displayLeaderboard() {
    const leaderboard = getLeaderboardFromLocal();
    const leaderboardElement = document.getElementById('leaderboard');

    leaderboardElement.innerHTML = ''; // Clear existing content

    leaderboard.forEach((entry, index) => {
        const row = document.createElement('div');
        row.textContent = `${index + 1}. ${entry.name} - ${entry.score}`;
        leaderboardElement.appendChild(row);
    });
}

// Example: Call displayLeaderboard to update the leaderboard
displayLeaderboard();

Then to implement it into the quiz you would just call on each function. Hope this helps you, please let me know if you need any further assistance! Good luck!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom