It says a variable is undefined even though I do define it


#1

it says Type Error: check is undefined. I don't understand why because I declare it on line 15. I'm probably just stupid and not seeing the obvious, but if someone could help, that would be great. and thanks in advance.

var baseDefence = 10;
var baseAttack = 10;
var baseHealth = 10;

//close equipment menu and update stats
var closeEquiptment = function() {
//equipment variables
var attack = baseAttack;
var defence = baseDefence;
var health = baseHealth;
var equipment = [woodBoots, ironSword, woodSword]; //the array that stores your equipment
var woodBoots = [0, 0, 10]; //says the stats in order of health, attack, defence
var ironSword = [0, 5, 0];
var woodSword = [10, 10, 10];
var check; //declares the check variable(this is the one that causes the problem)
i = 0;
//code
while(i < equiptment.length) { //goes through each item and adds the stats to your character
check = equiptment[i];
health = health + check[0];
attack = attack + check[1];
defence = defence + check[2];
i = i + 1;
}
console.log("attack " + attack);
console.log("health " + health); //logs your stats
console.log("defence " + defence);
};
closeEquiptment();

and yes, I know I spelt defense wrong.


#2

Huh. I've never seen this level before!:smile:

That's so cool!!!!!!!!

ANYWAYS,
@awsomedestroyers,

I think you should first put var before your i = 0, and in the var check; thing, I think you shhould not declare it yet, but declare it in the while loop you're using it with. Like, put var behind it.:smiley:

I've never seen a level like this, so I might be wrong. Where'd you find this anyway?! It's so cool!:smiley:


#3

it is a game I am making for a school project. it is a RPG type game with monsters, items and a planed traveling system. I am trying to make it as flexible as possible so people can type one line to add a new monster, item or place.


#4

I changed it so it looks like this, but I get the same error.

var woodSword = [10, 10, 10];
var i = 0;
//code
while(i < equiptment.length) {
var check;
check = equiptment[i];
health = health + check[0];
attack = attack + check[1];
defence = defence + check[2];
i = i + 1;
}


#5
var equipment = [woodBoots, ironSword, woodSword]; //the array that stores your equipment

A little typing mistake in that variable name.


#6

Nice job! It's actually kind of cool! Looks kind of Minecraft based, but cool!:smiley:


#7

nope, I'll post the whole updated code below. The error is the same I got at the beginning.

var baseDefense = 10;
var baseAttack = 10;
var baseHealth = 10;

//close equipment menu and update stats
var closeEquipment = function() {
//equipment variables
var attack = baseAttack;
var defense = baseDefense;
var health = baseHealth;
var equipment = [woodBoots, ironSword, woodSword];
var woodBoots = [0, 0, 10]; //health, attack, defense
var ironSword = [0, 5, 0];
var woodSword = [10, 10, 10];
var i = 0;
//code
while(i < equipment.length) {
var check;
check = equipment[i];
health = health + check[0];
attack = attack + check[1];
defense = defense + check[2];
i = i + 1;
}
console.log("attack " + attack);
console.log("health " + health);
console.log("defense " + defense);
};
closeEquipment();


#8

var baseDefense = 10;var baseAttack = 10;var baseHealth = 10;

//close equipment menu and update statsvar closeEquipment = function() {
//equipment variables
var attack = baseAttack;
var defense = baseDefense;
var health = baseHealth;
var woodBoots = [0, 0, 10];
//health, attack, defense
var ironSword = [0, 5, 0];
var woodSword = [10, 10, 10];
var equipment = [woodBoots, ironSword, woodSword];
var i = 0;//codewhile(i < equipment.length) { var check; check = equipment[i]; health = health + check[0]; attack = attack + check[1]; defense = defense + check[2]; i = i + 1;}
console.log("attack " + attack);
console.log("health " + health);
console.log("defense " + defense);
};
closeEquipment();

Try this


#9

thanks, works perfectly now :smile: