DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Relative Ranks - I

/**
 * @param {number[]} score
 * @return {string[]}
 */
var findRelativeRanks = function(score) {
    const sortedArray = [...score].sort((a,b) => b-a);
  let count = 1;
  let obj = {};
  for (let i = 0; i < sortedArray.length; i++) {
    obj[sortedArray[i]] = getValue(count);
    count++;
  }

    for(let i=0; i<score.length; i++){
        score[i] = obj[score[i]]?.toString()
    }
    return score
};

const getValue = (count) =>{

    switch(count){
        case 1:
            return "Gold Medal";
        case 2:
            return "Silver Medal";   
        case 3:
            return "Bronze Medal"
        default:
            return count;
    }

}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)