Created
March 21, 2017 14:23
-
-
Save evilboss/b3f07f573f274de6b2b4a87803b037ee to your computer and use it in GitHub Desktop.
Code Test: 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. Write your code in a secret gist (https://gist.github.com/) and paste the URL below.*
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
import _ from 'underscore'; | |
let ranks = [{name:'one',rank:1},{name:'two',rank:2}]; | |
let getByRanks = (ranks)=>{ | |
return _.sortBy(_pluck(ranks,'rank'), function(num) { | |
return num; | |
}) | |
}; | |
let getRankAverage = (ranks)=>{ | |
return _.reduce(_.pluck(ranks,'rank'), function(memo, num) { | |
return memo + num; | |
}, 0) / (arr.length === 0 ? 1 : arr.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment