Jump to content

Arrays - Leaderboard help requested


Recommended Posts

Hello,

 

I'm trying to make some kind of leaderboard, such as the example below:

 

var game = ["Dominoes",95,"Cards",22,"Chess",65];
The idea is to try to group all of the numbers together in the array so that I can return a position on them.
For example:
95 - would return 1st place (for the most points)
65 - 2nd place
22 - 3rd place
But the numbers in the "game" variable would also need to be linked in with the games so that if they were sorted numerically then the games would follow suit as well.
Would anyone be able to help with this, please? I'd be grateful for any assistance.
Many thanks,Dan.
Link to post
Share on other sites

A different data structure would be needed. Something like this, for example:

var game = [  { name: "Dominoes", score: 95 },  { name: "Cards", score: 22 },  { name: "Chess", score: 65 }];

Then you could use the sort() method and pass in a function that compares the scores of each item.

  • Like 1
Link to post
Share on other sites

A different data structure would be needed. Something like this, for example:

var game = [  { name: "Dominoes", score: 95 },  { name: "Cards", score: 22 },  { name: "Chess", score: 65 }];

Then you could use the sort() method and pass in a function that compares the scores of each item.

 

Thanks very much for this Ingolme, I'll give that structure a go.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...