Last active
January 30, 2017 08:02
-
-
Save hakimelek/dd957f2a15707c8230ba965e355e2e42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You have an array of objects in JavaScript. Each one contains a name (a string) and ranking (a number). | |
// Write two functions, one to return the objects ordered by ranking and another to return the average ranking. | |
var order = function (arr) { | |
return arr.sort(function (a, b) { | |
if (a.ranking > b.ranking) { | |
return 1; | |
} | |
if (a.ranking < b.ranking) { | |
return -1; | |
} | |
return 0; | |
}); | |
} | |
var avg = function (arr) { | |
var sum=0; | |
for(var i=0; i<arr.length; i++) { | |
sum += arr[i].ranking; | |
} | |
return sum/arr.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment