0

Sorry for the stupid question.
So I got this code here:

function stat(x) {
    player.ATK = 0; player.DEF = 0; player.DEX = 0; player.crit = 0; player.HP = 0;
    player.weaponATK = weapon.x.atk;
    player.weaponDEF= weapon.x.def;
    player.weaponDEX = weapon.x.dex;
    player.weaponHP = weapon.x.hp;
    player.weaponCrit = weapon.x.crit;
    player.ATK = player.baseATK + player.weaponATK;
    player.DEF = player.baseDEF + player.weaponDEF;
    player.DEX = player.baseDEX + player.weaponDEX;
    player.HP = player.baseHP + player.weaponHP;
    player.crit = player.baseCrit + player.weaponCrit;
}

And here's the whole var i use:

var player = {
  HP: 0,
  baseHP: 100,
  weaponHP: 0,
  ATK: 0,
  baseATK: 0,
  weaponATK: 0,
  DEF: 0,
  baseDEF: 0,
  weaponDEF: 0,
  DEX: 0,
  baseDEX: 0,
  weaponDEX: 0,
  crit: 0,
  baseCrit: 5,
  weaponCrit: 0,
  level: 1,
  currentEXP: 0,
  expLeft: 10
};
var weapon = {
  hatchet: {
    atk: 2,
    def: -1,
    dex: 0,
    hp: 0,
    crit: 0
  },
  woodenSword: {
    atk: 5,
    dex: 0,
    def: 0,
    hp: 0,
    crit: 0,
  },
  ironSword: {
    atk: 10,
    crit: 5,
    dex: 0,
    def: 0,
    hp: 0
  },
  blade: {
    atk: 25,
    crit: 20,
    dex: 10,
    hp: 0,
    crit: 0,
  },
  mace: {
    atk: 30,
    def: 5,
    dex: -1,
    hp: 0,
    crit: 0,
  },
  battleAxe: {
    atk: 50,
    def: 5,
    dex: 0,
    hp: 0,
    crit: 0,
  },
  broadSword: {
    atk: 100,
    def: 20,
    dex: 0,
    crit: 0,
    hp: 0
  },
  woodenShield: {
    atk: 0,
    def: 10,
    dex: 0,
    hp: 0,
    crit: 0,
  },
  spikeShield: {
    def: 15,
    atk: 5,
    dex: 0,
    crit: 0,
    hp: 0,
  },
  bomb: {
    atk: 0,
    def: 0,
    crit: 0,
    hp: 0,
    dex: -5
  }
};

And if I run stat(hatchet), it suppose to excute the function with x replace with hatchet. But instead, I got a error: "x is not defined". Can someone help me? Thanks for the help.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
AzDeveloper
  • 333
  • 1
  • 3
  • 14

3 Answers3

1

I assume that x is a property in weapon object, so you can use the bracket notation to access it from weapon like the following. Finally, when you call stat function, make sure that it is a string. See working code below:

var player = {
  HP: 0,
  baseHP: 100,
  weaponHP: 0,
  ATK: 0,
  baseATK: 0,
  weaponATK: 0,
  DEF: 0,
  baseDEF: 0,
  weaponDEF: 0,
  DEX: 0,
  baseDEX: 0,
  weaponDEX: 0,
  crit: 0,
  baseCrit: 5,
  weaponCrit: 0,
  level: 1,
  currentEXP: 0,
  expLeft: 10
};
var weapon = {
  hatchet: {
    atk: 2,
    def: -1,
    dex: 0,
    hp: 0,
    crit: 0
  },
  woodenSword: {
    atk: 5,
    dex: 0,
    def: 0,
    hp: 0,
    crit: 0,
  },
  ironSword: {
    atk: 10,
    crit: 5,
    dex: 0,
    def: 0,
    hp: 0
  },
  blade: {
    atk: 25,
    crit: 20,
    dex: 10,
    hp: 0,
    crit: 0,
  },
  mace: {
    atk: 30,
    def: 5,
    dex: -1,
    hp: 0,
    crit: 0,
  },
  battleAxe: {
    atk: 50,
    def: 5,
    dex: 0,
    hp: 0,
    crit: 0,
  },
  broadSword: {
    atk: 100,
    def: 20,
    dex: 0,
    crit: 0,
    hp: 0
  },
  woodenShield: {
    atk: 0,
    def: 10,
    dex: 0,
    hp: 0,
    crit: 0,
  },
  spikeShield: {
    def: 15,
    atk: 5,
    dex: 0,
    crit: 0,
    hp: 0,
  },
  bomb: {
    atk: 0,
    def: 0,
    crit: 0,
    hp: 0,
    dex: -5
  }
};

function stat(x) {
    player.ATK = 0; player.DEF = 0; player.DEX = 0; player.crit = 0; player.HP = 0;
    player.weaponATK = weapon[x].atk;
    player.weaponDEF= weapon[x].def;
    player.weaponDEX = weapon[x].dex;
    player.weaponHP = weapon[x].hp;
    player.weaponCrit = weapon[x].crit;
    player.ATK = player.baseATK + player.weaponATK;
    player.DEF = player.baseDEF + player.weaponDEF;
    player.DEX = player.baseDEX + player.weaponDEX;
    player.HP = player.baseHP + player.weaponHP;
    player.crit = player.baseCrit + player.weaponCrit;
}

// make sure x is a string!
stat('hatchet');

console.log(player)
Mμ.
  • 8,382
  • 3
  • 26
  • 36
  • will dot notation still work? Or bracket notaion is a must? – AzDeveloper Jun 03 '17 at 03:19
  • In this case, only bracket notation would work. Dot notation would only work if you are assigning a value to a known property in the object. For example, you want to update the player.baseHP, so you can do `player.baseHP = someVariable`. – Mμ. Jun 03 '17 at 03:23
  • I try your code, and it says "Cannot read property atk of undefine. – AzDeveloper Jun 03 '17 at 03:26
  • The code snippet above works, just try running the code snippet. That error means that weapon object is not defined in your case. – Mμ. Jun 03 '17 at 03:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145756/discussion-between-azdeveloper-and-d-reaper). – AzDeveloper Jun 03 '17 at 03:33
0

It's either what @brk or @D-reaper suggested

player.weaponATK = weapon.x.atk;

and similar need to be either

player.weaponATK = weapon[x].atk;

or just

player.weaponATK = x.atk;
m1kael
  • 2,801
  • 1
  • 15
  • 14
0

Since x is key passed to access the object property. It has to be used as weapon[x].atk

Nemani
  • 778
  • 5
  • 12