Javascript: Ice Hunter Help

  // Use the function isSubstring to check
    // if an enemy.id contains "bos":
    if (isSubstring == "bos"){
        // Then defeat it.  
        hero.attack(enemy);
    }
      
       

This does not work.
Should I not use an “if”?
Please tell me if I should post all of the code.

Could you please send me the link to this level?

Please post all your code.
Lydia

1 Like
// 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":
    if (isSubstring == "bos"){
        hero.attack(enemy);
    }
        // Then defeat it.
       
}

Here is the full code.

take a look at the name of the function.How many arguments does the function have?

In your if statement you have to use the function and its proper arguments. The correct arguments would be (word, substring). Your word is the enemy’s id (name), you can figure the substring yourself. All you need to do is check is if isSubstring(word, substring) is true or not. If it is, then attack the enemy. You also need to check if there is an enemy

1 Like

Continuing the discussion from Ice Hunter help Javascript:

I thought I couldn’t explain this so directly :smiley:, @abc

like, that would count as solution

I will edit my post a little.

@sci12, did all that help?

It helps as the hero moves but I still fail.

// 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":
    if (isSubstring == "bos"){
        hero.attack(enemy);
    }
        // Then defeat it.
       
}

New code.
Thanks!

Like I said earlier, you still haven’t used the proper arguments while calling your function.

Look what you are doing up there, and look at the function arguments down here:

You need to have isSubstring(word, "bos"). You can figure the word, as it tells you in the comments.

// 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":
   if (enemy) {
         if (isSubstring(enemy.id,"bos") === true){
        while (enemy.health > 0) {
          hero.attack(enemy);
        }
    }
   }
  
        // Then defeat it.
       
}

This is what I have but my hero dies.

Delete that part in the if statement checking for isSubstring(). Also delete the while statement afterwards.

How he dies?Like, does he dies because of an error or because not enough armor?

yeah the === operator means, that
isSubstring(enemy.id,“bos”) is identical to true
you need to use ==

btw you need to write True not true

I tried that but he still dies!

// 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.
hero.toggleFlowers(false);
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":
    if (enemy) {
        if (isSubstring(enemy.id, "bos")) {
            while (enemy.health > 0) {
               hero.attack(enemy);
               hero.shield();
            }
        }
    }    // Then defeat it.
}

here is the code.

Delete the while statement as well.

I checked your code in the level it works.You might have not enough armor.

What armor should I get?