简体   繁体   中英

About Leader Board in Javascript (Array) Help Me Please

I need help with a score table for my game.

-1- I have 4 variables:

var Player1Score= 44;
var Player2Score= 12;
var Player3Score= 45;
var Player4Score= 26;

--2-- i make a Array:

var MyArray=[Player1Score,Player2Score,Player3Score,Player4Score];

--3-- sort the array:

MyArray.Sort();

--4-- Print:

----------HIGHSCORES----------

                45

                44

                26

                12

MY QUESTION IS: HOW I CAN PRINT THE NAME OF THE PLAYERS IN ORDER¿?

LIKE THIS:

----------HIGHSCORES----------

PLAYER 3              45

PLAYER 1              44

PLAYER 4              26

PLAYER 2              12

THANKS IN ADVANCE. GREETINGS

Here's a jsfiddle that shows it http://jsfiddle.net/q2Wkh/1/

I first did the example you posted, that doesn't have any duplicates, that's only slightly tricky. You need to keep a map of which players had a particular score

console.log("\n\n Without duplicates\n\n");
var Player1Score= 44;
var Player2Score= 12;
var Player3Score= 45;
var Player4Score= 26;
var MyArray = [Player1Score,Player2Score,Player3Score,Player4Score];

// Create a map of score to player number
var scoreToPlayerNumber = {};
for (var i=0; i < MyArray.length; i++) {
  // Add 1 since player 1 is slot 0
  scoreToPlayerNumber[MyArray[i]] = "Player " + (i + 1) ; 
}
// You wanna sort backwards
MyArray.sort(function(a,b){return b-a});

for (var i=0; i < MyArray.length; i++) {
    console.log (scoreToPlayerNumber[MyArray[i]] + " : " + MyArray[i]);
}

To handle duplicate scores, your map that keeps track of which player scored what needs to be from the score to a list of players that had that score. I also improved variables names, which helps to think about the problem.

console.log("\n\n With duplicates\n\n");
var scores = [45, 43, 42, 48, 45];
var scoreToPlayerNumber = {};
for (var i=0; i < scores.length; i++) {
    var score = scores[i];
    if ( !scoreToPlayerNumber[score] ) {
        scoreToPlayerNumber[score] = [];
    }
    scoreToPlayerNumber[score].push("Player " + (i+1) );
}

scores.sort(function(a,b){return b-a});

// Some scores are in the list twice, don't print them twice
var seenScores = {};
for (var i=0; i < scores.length; i++) {
  var score = scores[i];
  if (!seenScores[score]) {
      var scorePlayers = scoreToPlayerNumber[score];
      for (var j = 0; j < scorePlayers.length; j++) {
         console.log(scorePlayers [j] + " : " + scores[i])
      }           
  }
  seenScores[score] = true;
}

And finally. The structure you're using could be improved to be more flexible. Here's a much simpler way that approaches it differently;

var players = [
  {name: "Player 1", score: 45},
  {name: "Player 2", score: 41},
  {name: "Player 3", score: 46},
  {name: "Player 4", score: 44},
  {name: "Player 5", score: 45},
  {name: "Player 6", score: 43},
  {name: "Player 7", score: 48}
];

players.sort(function(a,b){ return b.score - a.score});

for (var i=0, player; player = players[i]; i++) {
    console.log(player.name + " : " + player.score);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM