javascript - Logic for Gold, Silver, Bronze -


i'm building game in javascript , have results generated.

var score = array("5", "5", "1"); 

so player 1 has score of 5, player 2 score of 5 , player 3 score of 1.

i want display results 3 levels - gold, silver , bronze, if top 2 results same want them both gold, leaving lowest score silver.

what best way work out using javascript?

ideally results array particular set of scores this:

var results = array("gold", "gold", "silver"); 

thanks.

here's code doing it:

var scores = [5, 8, 1]; scores.sort(function(a, b) {     return (a > b) ? 1 : ((a < b) ? -1 : 0); }).reverse(); var medals = {}; var medalsused = 0; var availablemedals = ['gold', 'silver', 'bronze']; for(var = 0; < scores.length; i++) {     var score = scores[i];     var medal;     if(medals[score]) { // have medal score value? use         medal = medals[score];     }     else { // use next available medal         if(i >= 3) return; // not if we've given out 3 medals         medal = availablemedals.shift();         medals[score] = medal;     }      alert('medal ' + + ': ' + medal); } 

demo: http://jsfiddle.net/asdb8/1/


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -