# Ice hunter problem

**URL:** <https://discourse.codecombat.com/t/ice-hunter-problem/26038>\
**Category:** Level Help\
**Created:** [December 12, 2020, 7:46pm UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038 "2020-12-12T19:46:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11193](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/11193/32/16859_2.png) [@11193](https://discourse.codecombat.com/u/11193)\
**Post date:** [December 12, 2020, 7:46pm UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/1 "2020-12-12T19:46:37Z")

</div>

The hero attack only 2 yaks.Why??

```python
// Hunt for 4 yaks. Choose only the small ones.
// Small yak names contain a "bos" substring.

// This function checks if a word contains a substring.
function isSubstring(word, substring) {
    // We iterate through the start indexes only.
    var rightEdge = word.length - substring.length;
    // Loop through the indexes of the word.
    for (var i = 0; i <= rightEdge; i++) {
        // For each of them loop through the substring
        for (var j = 0; j < substring.length; j++) {
            // Use an offset for the word's indexes.
            var shiftedIndex = i + j;
            // If letters aren't the same:
            if (word[shiftedIndex] != substring[j]) {
                // Check the next start index in the word.
                break;
            }
            // If it was the last letter in the substring:
            if (j == substring.length - 1) {
                // Then the substring is in the word.
                return true;
            }
        }
    }
    // We haven't found the substring in the word.
    return false;
}

// Loop through all enemies.
var enemies = hero.findEnemies();
for (var e = 0; e < enemies.length; e++) {
    var enemy = enemies[e];
    // Use the function isSubstring to check
    // if an enemy.id contains "bos":
    let enemyName = enemy.id;
    hero.say(enemyName);
    if (isSubstring(enemyName, "bos")) {  
            while (enemy.health > 0) {
                hero.attack(enemyName);
            }   
    }
        // Then defeat it.     
}

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [December 13, 2020, 8:20am UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/2 "2020-12-13T08:20:46Z")

</div>

Hi 11193,

Your code works when I try it - my hero kills 4 yaks. I’m using Anya, and didn’t lose much health, so I’m not sure why you didn’t succeed. Try submitting again?

Jenny

---

<div class="post-metadata">

**Author:** ![11193](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/11193/32/16859_2.png) [@11193](https://discourse.codecombat.com/u/11193)\
**Post date:** [December 13, 2020, 8:55am UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/3 "2020-12-13T08:55:34Z")

</div>

> [@jka2706](#):
>
> Try submitting again?

yes, many times. It didn’t help(( I tried to change heroes too…

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [December 13, 2020, 10:49am UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/4 "2020-12-13T10:49:50Z")

</div>

Hmmm, interesting. Does the hero say the enemy names with ‘bos’ in them, and not attack; or not say the names at all?

Jenny

---

<div class="post-metadata">

**Author:** ![11193](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/11193/32/16859_2.png) [@11193](https://discourse.codecombat.com/u/11193)\
**Post date:** [December 13, 2020, 3:33pm UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/5 "2020-12-13T15:33:03Z")

</div>

he say the names but attack only 2 yaks

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [December 13, 2020, 5:33pm UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/6 "2020-12-13T17:33:28Z")

</div>

Ha, after trying several times I got the code to fail - only 3 yaks got attacked, and the hero didn’t say the name of the fourth one. I wonder if there’s an issue with the fact that when the array gets constructed, the yak(s) in question are out of sight of the hero?

Solution is (I hope) easy - just put the code into a while(true) loop, so that the hero checks all the yak names again, and carries on hunting. Does that work for you?

Jenny

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [December 14, 2020, 6:38pm UTC](https://discourse.codecombat.com/t/ice-hunter-problem/26038/7 "2020-12-14T18:38:41Z")

</div>

Closed on December 14, 2020, 6:38pm UTC

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