Hi guys!
Quick question as a somewhat new JavaScript scholar!
So even though this code works fully, there’s something that bothers me and I’d like to ask some explanation about it:
while (true) {
var enemy = hero.findNearestEnemy();
var item = hero.findNearestItem();
if (enemy && enemy.type == "munchkin") {
hero.attack(enemy);
}
if (item && item.type == "coin") {
hero.moveXY(item.pos.x, item.pos.y);
}
}
It’s for a level that requires 8 or less statements, so I’m gonna leave it like this without adding more variables. It’s rather redundant for my question any how… So the question is:
if (enemy && enemy.type == "munchkin") {
hero.attack(enemy);
}
How I understand this line is: If enemy exists && enemy type is “munchkin” – attack nearest enemy
However, the game seems to run it as: if enemy exists && enemy type is “munchkin” – attack “munchkin”
But where is the attack “munchkin” defined? To my understanding (though it doesn’t work, obviously) I’m still just reading “hero.attack(hero.findNearestEnemy)” … Does the line on top of it automatically define that it needs to be a “munchkin” to be attacked, because it checked if “munchkin” exists before? … If “munchkin” exists, attack the nearest enemy which could just as well be an ogre…
Hi @AroenvR , welcome to the CodeCombat Discourse.
I’m not exactly sure what to explain, so I’m going to go wide and hope I’ve explained what you want to know.
The variable `enemy` was defined on line 2 as your nearest enemy. `Enemy` is an object, meaning, in this case, a thing in the game. On line 4 you first check whether `enemy` exists. Is there a registered enemy of any type within your glasses' range of vision? If so, it then checks whether it's "type" property (which is a string, meaning text in "") is "munchkin". Type is just another property of an enemy object, like maxHealth, or distanceTo(). When you declare it, it returns a string. e.g. "munchkin", "ogre", "thrower", "shaman", etc...
So if the enemy’s type is “munchkin”, the code will continue to the next line, and attack the enemy. But it is attacking the object, which is a number in an array (If you haven’t done them already you will in the desert levels.). You are not attacking a type of enemy you are attacking a specific one, e.g. Krakk 5. Or Ongul 2.
If the enemy’s type is not “munchkin”, let’s say it’s “burl” (I think I might know what level you’re on, but maybe not), you won’t attack it, because its type isn’t “munchkin”. So the burl will stay as your nearest enemy, even if there’s a munchkin a little bit further away. Then if your burl moves on and the munchkin becomes your nearest enemy, you will then attack it.
So, simply, I was thinking about this wrong, and thank you very much for clearing it up!
You explained it quite perfectly…
I was thinking about if (enemy && enemy.type == "munchkin") and hero.attack(enemy); as any general enemy, not the NEAREST enemy … And that’s where my mind went wrong… I used to work with a different API and I would have to write if (enemy.exists && enemy.type == "munchkin") then hero.attack(enemy.type == "munckin") otherwise it would attack any enemy as long as a munchkin exists… But hero.findNearestEnemy is checking the NEAREST enemy only, which I completely forgot about! (even though it’s in the name haha)
I thank you once again!
AroenvR
P.s.: I’m going to be starting with Arrays in the Desert levels tomorrow! Looking forward to it!