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

Explanation needed for highlanders

#21

@mattgamer So you are lucky that all of the names that shouldn’t have been called to battle did not have a “c” in them.

If you would like to keep working on the problem there are a few more things we can try to perhaps make it more clear what is happening.

What web browser are you using? I ask because you will need to look at the console output.

Use notepad or another text editor to save your code so far and use the following for the loop code:

console.log("====== name : " + string + " word: " + word);
for ( var i = 0; i < lenString; i++) {
        console.log("============= Outer Loop: i= " + i);
        for (var j = 0; j < lenWord; j++) {
           console.log("Inner loop: i = " + i + " j = " + j + " (i + j) = " + (i + j) + " string[i]: " + string[i] + " string[i+j]:" +  string[i+j] + " word[j]; " + word[j] + " word[lenWord -1]: " + word[lenWord - 1] );
            if (string[i] == word[lenWord - 1]) {
                console.log("======= Matched string[i] to word[2]");
            }
        }
    }
}
#22

I use chrome
now what is that code for?

#23

@mattgamer the code will output variables as the loop plays and let you know their values at each step in the loop.

Have you opened up the console before? Use the above link or do a J to bring it up. The output will be in the console. Hence the console.log();

#24

I get the error “illegal return statement”

#25

ah.

I removed the return false segment in the code above. Try it without that.

#26

I need help with everything at the beginning, with looping like:
for i in range(lenString - lenWord)
I don’t even know what it’s telling me to do!

#27

Hi there,
I was stuck at this level for days. Even with your explenation @Harry_the_Wanderer I just didn’t get it.
But just now I found the answer to the Level, the console Log helped a lot.
The problem is (j == lenWord - 1). The j is also part of the lenWord - 1 part.
I hope this hint helps.
And thanks to Harry for so much afford I appreciate that.

#28

@Tobias_Kreuzig you are welcome. I wanted to help everyone work “through” the problem and not just copy-cut-paste.


[quote="Ninja_Emboar, post:26, topic:9220"] ```python for i in range(lenString - lenWord) ``` [/quote] @Ninja_Emboar so there are several things happening here.
  1. Do you understand for loops and their purpose?
  2. Do you understand the range() function
  3. Do you understand the purpose behind the mathematics involved with the calculation lenString - lenWord
#29

here is your above code example. somehow when I copied it the brackets were off, and obviously variables weren’t declared. This here can be copied straight to console (for example in chrome developer tools, by pressing F12 or right click -> “inspect”) to see the log in action :slight_smile:


var string = "macaffe", word = "affe", lenString = string.length, lenWord = word.length;
console.log("====== name : " + string + " word: " + word);
for ( var i = 0; i < lenString; i++) {
    console.log("============= Outer Loop: i= " + i);
    for (var j = 0; j < lenWord; j++) {
       console.log("Inner loop: i = " + i + " j = " + j + " (i + j) = " + (i + j) + " string[i]: " + string[i] + " string[i+j]:" +  string[i+j] + " word[j]; " + word[j] + " word[lenWord -1]: " + word[lenWord - 1] );
        if (string[i] == word[lenWord - 1]) {
            console.log("======= Matched string[i] to word[2]");
        }
    }
}