Is there a more elegant way of writing a function that returns the name of an image based on the argument of the function?

You can use a plain Object as a map.

var emblems = {
  IRON: "Emblem_Iron",
  BRONZE: "Emblem_Bronze",
  SILVER: "Emblem_Silver",
  GOLD: "Emblem_Gold",
  PLATINUM: "Emblem_Platinum",
  DIAMOND: "Emblem_Diamond",
  MASTER: "Emblem_Master",
  GRANDMASTER: "Emblem_Grandmaster",
  CHALLENGER: "Emblem_Challenger"
};

function rankedEmblem(tier) {
  // Could also help user by doing:  emblems[tier.toUpperCase()]
  return emblems[tier] || "No_Emblem";
}

console.log(rankedEmblem("GOLD"));
console.log(rankedEmblem("PLATINUM"));


You can use String concatination and functions like so:

function rankedEmblem(tier){
    return "Emblem_" + tier.charAt(0).toUpperCase() + tier.slice(1).toLowerCase();
}

console.log(rankedEmblem("CHALLENGER"));


rankedEmblem(tier){
  let myEmblems = {IRON: 'Emblem_Iron', BRONZE: 'Emblem_Bronze' }; 
  //You can put whatever emblem you want as key:value pair
  return myEmblems[tier];
}

Tags:

Javascript