Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda