0

I'm trying to write a basic function which allows a user to enter a category (either game or player) and a number within that category to return either the player name or the game name.

var objName = "";

var playerObj = {
  12: "Tasneem",
  16: "Kevin",
  19: "Bevan"
};

var gameObj = {
  22: "Tetris",
  56: "Mario",
  16: "Donkey Kong"
};

function playerOrGame(prefix, num) {
  objName = prefix + "Obj";
  return objName[num];
};
 
console.log(playerOrGame("player", 16))
 

In the above code,

playerOrGame("player", 16) 

should return "Kevin". What am I doing wrong?

  • 3
    "playerObj"[16] - This is basically what you're returning. The 16th position in a string. Instead of `playerOrGame("player", 16)`, try `playerOrGame("player", 6)`. – Rickard Elimää Jul 06 '21 at 09:51
  • Thank you I see what you're saying and understand that problem now. What is the solution to the function returning undefined where it should return a player or game name? The goal is for the function to return the name of a player or game when the only information you have is: 1. whether they're a player or game 2. their number in the list There is neither a player nor game number 6 in either object. I'm trying to find the player name for player 16 (or game 22 or whatever) – John Bartmann Jul 06 '21 at 09:53
  • 1
    [“Variable” variables in JavaScript](https://stackoverflow.com/q/5187530) – VLAZ Jul 06 '21 at 09:53
  • 3
    I don't see the point of the `playerOrGame` function. It fetches an index from either one object or another and it's *the caller* who needs to know which one. So `playerOrGame("player", 16)` is the same as calling `playerObj[16]` but with more work and chance of mistake. The *only* benefit might be if you don't want to expose the `playerObj`/`gameObj` variables but it's probably easier to just expose two functions - one that looks up a player and one that looks up a game. The caller will then call the appropriate one as they already know whether they want a player or a game. – VLAZ Jul 06 '21 at 09:57
  • Duplicate of [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – MrUpsidown Jul 06 '21 at 10:09

2 Answers2

0

There can be a dirty solution to this depending on the environment you run this in.

In browsers, all global variables(using var) are attached to the window object and you can make use of that fact. This will also work in strict mode.

For instance, in the below code you can access your object like: window["playerObj"] and window["gameObj"]

var objName = "";

var playerObj = {
  12: "Tasneem",
  16: "Kevin",
  19: "Bevan"
};

var gameObj = {
  22: "Tetris",
  56: "Mario",
  16: "Donkey Kong"
};

function playerOrGame(prefix, num) {
  objName = prefix + "Obj";
  return window[objName][num];
};
 
console.log(playerOrGame("player", 16))

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • Thanks. Running this in the FreeCodeCamp tutorial sandbox and it returns window as undefined. – John Bartmann Jul 06 '21 at 10:05
  • Yeah. That is why i mentioned depending on the env. They probably do not want you to manipulate the window object. I was under the assumption you specifically wanted global variables. This will run well on your chrome console and usual front end wesbites. – Tushar Shahi Jul 06 '21 at 10:09
0

You can use JavaScript's built-in eval() method to have dynamic variable names.

var objName = "";

var playerObj = {
  12: "Tasneem",
  16: "Kevin",
  19: "Bevan"
};

var gameObj = {
  22: "Tetris",
  56: "Mario",
  16: "Donkey Kong"
};

const varToString = varObj => Object.keys(varObj)[0]

function playerOrGame(prefix, num) {
    
  objName = prefix + "Obj";
  let newVar = eval(objName)
  return newVar[num];
};
 
console.log(playerOrGame("player", 16))

You can find more about evals here: When is JavaScript's eval() not evil?

ahsan
  • 1,409
  • 8
  • 11