Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Clash of Clones, Javascript {SOLVED}

#22

The main question I have is with the first line:

var index = 0;

Do I really need the var? When I remove it, the error box tells me “Missing var. Use var index = to make a variable.”

#23

I think CodeCombat is running in JavaScript “strict mode”:

The "use strict" directive was new in ECMAScript version 5.
It is not a statement, but a literal expression, ignored by earlier
versions of JavaScript. The purpose of "use strict" is to indicate
that the code should be executed in "strict mode". With strict 
mode, you can not, for example, use undeclared variables.
All modern browsers support "use strict" except Internet Explorer 9 and lower:

If your python code is successful I don’t think you rewrote it right in JS. Try to follow the logic of your original code and use the same formatting as in python.

    if (index < archers.length) {
        while (index < archers.length) {
            index += archers;
        }
    }

here you get the archers[archers.length] warrior - why are you doing this?

#24

Originally, I was planning to use

for (archer in archers) {

when I used this statement, I decided to do something else, and I never deleted the

if (index < archers.length) {
    while (index < archers.length) {
            index += archers;
    }
}

I will delete it now since it’s essentially a useless line of code. And thank you for responding so quickly!!!

#25

Would someone please compare this code and see if I translated it from Python to JS correctly? It would be very appreciated!!!

Python:


def findArcher(enemies):
    for enemy in enemies:
        if enemy.type == "archer":
            return enemy
while True:
    enemies = hero.findEnemies()
    archer = findArcher(enemies)
    if archer:
        if self.isReady("bash"):
            hero.bash(archer)
        else:
            while archer:
                archer = findArcher(enemies)
                hero.shield()
                if self.isReady("bash"):
                    hero.bash(archer)

JavaScript:

function findArcher(enemies) {
    for (var enemy in enemies) {
        if (enemy.type == "archer") {
            return enemy;
        }
    }
}
while (true) {
    var enemies = hero.findEnemies();
    var archer = findArcher(enemies);
    if (archer) {
        if (hero.isReady("bash")) {
            hero.bash(archer);
        } else {
            while (archer) {
                hero.shield();
                if (hero.isReady("bash")) {
                    hero.bash(archer);
                }
            }
        }
    }
}

One last thing - I’m not getting any errors with the JavaScript code, but my hero won’t do anything at all, so there must be some other issue. Thanks to anyone who can tell me how I translated it incorrectly!

#26
# python
            while archer:
                archer = findArcher(enemies)
                hero.shield()
// js
            while (archer) {
                // missing statement
                hero.shield();

#27

Thanks, I’ll try this!!!

#28

It still doesn’t work! I think that my main issue is with the

for (var enemy in enemies) {

statement. I feel like it’s not supposed to have var. Correct me if I’m wrong though.

#29

Yes, you are wright, the proper construction is:

    for (var i in enemies) {
        var  enemy = enemies[i];
         // your code 
         // you can make it without declaring var
         // if (enemy[i].type == "archer")

        }

#30

Thank you for helping me out! I’ve been away from Discourse for a while, so I just saw this, but thanks!

#31

I passed the level after putting in this code and equipping the Monolith shield. Thanks for all your help!

1 Like