Skip to content

Instantly share code, notes, and snippets.

@evilboss
Created March 21, 2017 14:23
Show Gist options
  • Save evilboss/b3f07f573f274de6b2b4a87803b037ee to your computer and use it in GitHub Desktop.
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.*
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