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.
3 Answers 3
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)
5 Comments
player.baseHP = someVariable.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;
Comments
Since x is key passed to access the object property. It has to be used as
weapon[x].atk
Comments
Explore related questions
See similar questions with these tags.
hatchetdefined as?xbut you are trying to accessweapon.xweapon.xas parameter. Check if it works.