Pesky Yaks PLEEAAASE help!

So, I don’t really know how to use the “push” method so I just did hero.push() and it’s giving me an error that says the hero has no method “push” and I don’t know what to do with it!! Here is my code:

// That's a lot of Yaks!
// If you are to survive, you'll need to filter out Yaks...

function removeByType(enemies, excludedType) {
    var tempArray = [];
    // Go through each enemy and check to see if its type is excludedType.
    for(var i = 0; i < enemies.length; i++) {
        // If it isn't, 'push' it onto the array.
        let enemy = hero.findNearestEnemy();
            if (enemy.type != "sand-yak") {
                hero.push(enemy);
            }
    }
    return tempArray;
}


while(true) {
    // Find the enemies!
    var enemies = hero.findEnemies();
    // Remove those pesky Yaks.
    enemies = removeByType(enemies, "sand-yak");
    var enemy = hero.findNearest(enemies);
    // Now... 'remove' those enemies.
    if(enemy) {
        hero.attack(enemy);
    }
}

Instead of hero.push, put the name of the array you want the element to go in.
For example, array.push(element) would put the element inside the array.

so you are saying that I have to put an array with the push statement?

Yes, in this case tempArray

wait–what’s a “temp array”?

I just meant the array you were using to store the enemies in.
var tempArray = []

Okay I got it. Thanks A lot. You really helped me

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.