Question

heres my code:

function insertMonsters() {

var monsterPositions = [2,8,13,15,22];

for ( var i in monsterPositions ) {
    monsters[i] = createMonster("monster",monsterPositions[i],1);
    document.getElementById(monsters[i].currentPosition).innerHTML = monsters[i].displayText;
    }

}

function createMonster(name,startingPoint,level){
    this.displayText = "<span>" + name + "</span>";
    this.currentPosition = startingPoint;
    this.level = level;
    this.health = function(){ return 25 + ( this.level * 15 );};
    this.strength = function(){return ( this.level * 3 );};
}

and i get a error:

TypeError: createMonster(...) is undefined
[Break On This Error]   

monsters[i] = createMonster("monster",monsterPositions[i],1);

and i kinda really got no clue whats wrong, link here help please.

Was it helpful?

Solution

It looks like its monsters thats undefined, not createMonster. Make sure to initialize the monsters array and you should be ok.

You can change line 7 to

var monsters = [];

you're also missing a new statement when setting the monster,

you need

monsters[i] = new createMonster("monster",monsterPositions[i],1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top