{SOLVED}Highlanders JS Cs5

I have been working on it for half an hour, but I can’t figure out what’s wrong.

// You must defeat the ogres
// But they are using black magic!
// Only the highlander soldiers are immune.
// Find highlanders, their names always contain "mac"

var highlanderName = "mac";

// This function should search for a string inside of a word:
function wordInString(string, word) {
    var lenString = string.length;
    var lenWord = word.length;
    // Step through indexes (i) from 0 to (lenString - lenWord)
    for (var i = 0; i< lenString-lenWord; i++) {

        // For each of them step through indexes (j) of the word length
        for (var j = 0; j<lenWord; j++) { 

            // If [i + j]th letter of the string is not equal [j]th letter of word, then break loop

            if(string[i+j] != word[j]) {

                break;
            }
        }
            
            // if this is the last letter of the word (j == lenWord - 1), then return true.
        if(j == lenWord - 1) {
            return true;
        }
        
    // If loops are ended then the word is not inside the string. Return False.
    }
    return false; 
}

// Look at your soldiers and choose highlanders only
var soldiers = hero.findFriends();
for (var i = 0; i < soldiers.length; i++) {
    var soldier = soldiers[i];
    if (wordInString(soldier.id, highlanderName)) {
        hero.say(soldier.id + " be ready.");
    }
}

// 
hero.say("ATTACK!!!");

Please help.

Try switching the soldier.id and highlanderNamrle

highlanderName*
I don’t think that will help tho, but might as well try
(also, I have no idea what’s wrong here)

To compare, this is the base code.

// You must defeat the ogres
// But they are using black magic!
// Only the highlander soldiers are immune.
// Find highlanders, their names always contain "mac"

var highlanderName = "mac";

// This function should search for a string inside of a word:
function wordInString(string, word) {
    var lenString = string.length;
    var lenWord = word.length;
    // Step through indexes (i) from 0 to (lenString - lenWord)
    
        // For each of them step through indexes (j) of the word length
        
            // If [i + j]th letter of the string is not equal [j]th letter of word, then break loop
            
            // if this is the last letter of the word (j == lenWord - 1), then return true.
            
    // If loops are ended then the word is not inside the string. Return False.
    
    return true; // ∆ Remove this when the function is written.
}

// Look at your soldiers and choose highlanders only
var soldiers = hero.findFriends();
for (var i = 0; i < soldiers.length; i++) {
    var soldier = soldiers[i];
    if (wordInString(soldier.id, highlanderName)) {
        hero.say(soldier.id + " be ready.");
    }
}

// 
hero.say("ATTACK!!!");
1 Like

I don’t think it will work, unless the base level code is wrong. I can try anyway.

It didn’t work. (20 charrs)

If you have a wizard, and elemtal codex you can swap the nearest soldier and then join the battle

2 Likes

Nice exploit, but maybe don’t post it in topics? :]

1 Like

Fun idea, but I don’t have a wizard and I like trying to do the level properly.

I found the problem! Thanks y’all!

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