Given an array of unique numbers, return a new array of rankings of the original array. For example, if your input array is [10, 5, 20], the output should be [2, 3, 1], since 10 is the second-largest number, 5 is the third largest, and 20 is the largest.
My answer is returning [1,2,0] rather than [2,3,1]. I can't figure where I'm going wrong?
function rankings(arr) {
// add whatever parameters you deem necessary
var sorted = arr.slice().sort(function(a, b) {
return b - a
})
var ranks = arr.slice().map(function(v) {
return sorted.indexOf(v)
});
return ranks;
}
console.log(rankings([10, 5, 20]));