Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Table with unsorted numbers in JavaScript

I have made a leaderboad that shows the scores of the users from the local storage. But It isn't in the order of the scores (highest to lowest). Also, I want to have only up to 10 users shown at a time in the leaderboard. I thought about using a for loop for making it stop at 10, but that just printed every user 10 times.

index.html

    <table border="2" id="Ranking" class="ldr"></table>
    <script> 
    loadRankingTable();
    window.onload = () => {
    if (sessionStorage.loggedInUser !== undefined) {
    let oldData = localStorage.getItem(sessionStorage.loggedInUser);
    if (oldData) {
        oldData = JSON.parse(oldData);
        oldData.topScore = highscore;
        localStorage.setItem(sessionStorage.loggedInUser, JSON.stringify(oldData));
    }   
    document.getElementById("Greeting").innerHTML = sessionStorage.loggedInUser;
    }
    }

    </script>

prac.js

function loadRankingTable(){
    let str = "<table><tr><th>Rank</th><th>Name</th><th>Score</th></tr>";
    for(let key of Object.keys(localStorage)){
        let usr = JSON.parse(localStorage[key]);
        if (key !== "highscore"){
                str += "<tr><td>" + "1" + "</td><td>" + usr.username + "</td><td>" + usr.topScore + "</td></tr>";
        }
    }
    str += "</table>";
    document.getElementById("Ranking").innerHTML = str;
}

this is the stored data (ignore the key highscore)

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!