Skip to content

Instantly share code, notes, and snippets.

@salvianoo
Created September 29, 2015 00:41
Show Gist options
  • Save salvianoo/33b5f474e6451775973f to your computer and use it in GitHub Desktop.
Save salvianoo/33b5f474e6451775973f to your computer and use it in GitHub Desktop.
// The problem
//
// Get the player with greater score
var players = [
{name: 'Steve', score: 6.2},
{name: 'Bruce', score: 7.2},
{name: 'Peter', score: 5.4},
];
function compareScore(p1, p2) {
return (p1.score > p2.score) ? p1 : p2;
}
// Imperative style
var greater = players[0];
for (var i = 1; i < players.length; i++) {
greater = compareScore(players[i], greater);
}
console.log( greater );
// Functional style
var winningPlayer = players.reduce(compareScore);
console.log( winningPlayer );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment