Search Text For Your Name - Weird result but passed the lesson


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661


/*jshint multistr:true */
var text = "rena dhaskjhd iwye rena bdajksh gdasjk dbasa rena\
            hdsakl rena re dgskaj";
var myName = "rena";
var hits= [];
for (var i=0; i < text.length; i++){
    if(text[i] === "r"){
        for (var j=i; j < i+myName.length; j++);
             hits.push(text[j]);
         }      
}
if(hits.length === 0){
    console.log("Your name wasn't found");
}
else{
    console.log(hits);
}

i got : [ ' ', ' ', ' ', ' ', 'g' ] as a result but i passed the lesson. Is that the right result?


#2

Hey.

Your for loop where you push text[j] is currently without opening and closing braces.


#3

nothing's changed. still got the same result :confused:


#4

Make sure you delete that semi-colon after for (var j=i; j < i+myName.length; j++); because that would stop the block code (which should contain hits.push(text[j]); from running according to the logic of the for loop.

The code should look like:

for (var i=0; i < text.length; i++){
    if(text[i] === "r"){
        for (var j=i; j < i+myName.length; j++){
             hits.push(text[j]);
        {
    }     
}

#5

i got it! thanks :slight_smile:


#6

just in case you (or anyone else) were wondering why you got that output let me attempt to explain: [ ' ', ' ', ' ', ' ', 'g' ]

Lets call the second for loop (the one with the j variable) namefor loop.

The first for loop

The first for loop's job is to basically go through every character on the text array until the if(text[i] === "r") is true. Once an "r" is found the namefor loop triggers.

The second for loop (namefor Loop)

Let's start with the first bit of logic in the nameforloop parameters: j = i;
This sets j equal to the value of 0 because i in this case is equal to the first index value in the array in which the character "r" was found.


Our next bit of logic is. j < i+myName.length;.
This lets the namefor loop run as long as we don't go over the necessary amount of letters in the name we are searching (myName). Because we found the first letter at index 0 we want to tell this loop to stop once we reach index 4 because index 4 does not contain any letters in the name we want to push anymore (it's a space). We only want to include index 0(r), index 1(e), index 2(n), and index3(a). So that's fine; this for loop will stop once it gets to index 4.

And as you should know j++ simply increments j after hits.push(textpj]); is executed.
Except it doesn't (dun dun dun)


Now because that semi-colon's is where it is (just after j++)). It's stopping the namefor loop from properly executing any actual code. So instead what it's doing now is incrementing j and reaching index 4 before it pushes anything out to the hits[] array.

Therein lies the problem because once it reaches index 4 the next line of code hits.push(text[j]); can now run. And what is the value of j in the text array? It's " ". That's why you get 4 " "'s because every rena has a space after it.

This is why it's really important to include the code we want to run in between these --> { }. Because it tells the for loop to run whatever is inside there first before incrementing and producing weird results.


As for the last output which is "g". I'll leave that for you to see if you can figure out why it prints that :slight_smile:


I hope you got something from that. I was in an explaining mood.


#7

Wow! Thanks for your explanation! It really help me to understand more about the code! As for the "g" result, if I'm not wrong, it comes from "re dgskaj", right? Because the first letter is match with the if condition, so the program just read it as one, and the "g" letter was read as index 4?


#8

Yep exactly right :slight_smile:


#9

You did better than I did. I was trying to make it pass with hits.push(); alone. I forgot about the text[j] part!


#10

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