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
52.5k10 gold badges120 silver badges166 bronze badges
asked Jun 3, 2017 at 2:49
6
  • what is hatchet defined as? Commented Jun 3, 2017 at 2:52
  • Possible duplicate of Dynamically access object property using variable Commented Jun 3, 2017 at 2:52
  • 1
    the parameter is x but you are trying to access weapon.x Commented Jun 3, 2017 at 2:53
  • @PatrickRoberts hatchet is define as a variable Commented Jun 3, 2017 at 2:55
  • Just simply send weapon.x as parameter. Check if it works. Commented Jun 3, 2017 at 3:08

3 Answers 3

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)

answered Jun 3, 2017 at 2:55
Sign up to request clarification or add additional context in comments.

5 Comments

will dot notation still work? Or bracket notaion is a must?
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.
I try your code, and it says "Cannot read property atk of undefine.
The code snippet above works, just try running the code snippet. That error means that weapon object is not defined in your case.
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;
answered Jun 3, 2017 at 2:58

Comments

0

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

answered Jun 3, 2017 at 4:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.