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

hero.findEnemies() Quiz


#1

You have the following piece of code:
java script:

var enemies = hero.findEnemies();
if (enemies)
    hero.say('So many enemies!');
else
    hero.say('No enemies!');

or python:

enemies = hero.findEnemies()
if enemies:
    hero.say('So many enemies!')
else:
    hero.say('No enemies!')

What will hero say if there are no enemies?

You can test the it in https://codecombat.com/play/level/logical-path? for python or
in https://codecombat.com/play/level/logical-path?codeLanguage=javascript for java script.
You don’t need to be logged in, no glasses needed for hero.findEnemies() in any level in codecombat.
Simply comment the default ( or your ) code and paste the above code.


#2

The hero will say “So many enemies!”, since the enemies array exists, even if there are no enemies


#3

And how the code must be rewritten to work as intended? ( question not to @SuperSmacker , I know he knows the answer :wink: )


#4

The easiest way I can think of (python)…

Spoiler alert

Instead of: if enemies:,

change to: if len(enemies) > 0:


#5

Yes, this is the way this check must be done. ( if enemies.length > 0: will also work in CoCo python )
I have made a search in topics and there are many examples where this error is present. But the catch is the CoCo python isn’t acting as a real python.
If there are no enemies the answer must be ‘No enemies!’ after if enemies: in real python.
see Enemy = Null, filter confusingly functioning (Solved)