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.”
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.”
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?
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!!!
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!
# python
while archer:
archer = findArcher(enemies)
hero.shield()
// js
while (archer) {
// missing statement
hero.shield();
Thanks, I’ll try this!!!
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.
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")
}
Thank you for helping me out! I’ve been away from Discourse for a while, so I just saw this, but thanks!
I passed the level after putting in this code and equipping the Monolith shield. Thanks for all your help!
// You’ll need good strategy to win this one!
// Your clone will have the same equipment you have!
// But, they’re not very skilled at using special powers.
// Stay alive longer than the enemy hero!
// You’ll need good strategy and good equipment to win this one!
while (true) {
var enemies = hero.findEnemies();
var enemyArchers = hero.findByType("archer", enemies);
var friends = hero.findFriends();
var enemy = hero.findNearestEnemy();
if (enemyArchers.length > 0) {
enemy = hero.findNearest(enemyArchers);
while (enemy.health > 0) {
hero.attack(enemy);
}
} else if (hero.pos.x > 50) {
hero.moveXY(50, hero.pos.y);
} else if (hero.health < 1500 && friends.length > 6) {
hero.shield();
} else if ((enemy.type != "sand-yak")) {
while (enemy.health > 0) {
hero.attack(enemy);
}
}
}
I died using this code
But I won after submiting again
You can put this code:
if (hero.health <= 100) {
//Put the run-away code here.
}
Welcome to the forum!! Please do not revive dead topics as BrendGroen is not active anymore so you are wasting your post