Help with "Your second "for" loop


#1


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


SyntaxError: Unexpected token ';'. Expected ')' to end a compound expression.


/*jshint multistr:true */

var text = "Hey, how are you \
doing? Akshar. You are looking amazing. \
Where do you work, Akshar?";

var myName="Akshar";

var hits=[];

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


#3

Missing parentheses after myName.length


#4

Thanks. It works now!


#5

Thanks. I notice my error.


#6

Always happy to help :slight_smile:


#7

Hey i am still trying to understand the code. The way i am looking at it is this way, text[24] is where the code sees the first letter of my name. That text[24] becomes letter A and it is being checked with myName[0] = A <--first letter of my name. The second for loop then takes the i=24 into j=24 which loops till the last length of myName so here it is j<30. testing a console.log{text[j]); would give me the full letters of my name AKSHAR. Just curious and i know the code is correct and works when adding this in the second for loop----> hits.push(text[j]);} giving me 12. But i don't know WHY. Why would it not check the letter 'a' in the text at text[9] which is also an 'a'.Though, it doesn't fully equal myName since the second letter becomes 'r'. Not sure if this is confusing to explain to you. But an explanation of why or how the code above works would be great. Thanks.


#8

Great explanation so far, I'm not entirely sure myself but your if condition uses the === comparison operator. I think it will only execute if parameter type matches exactly, in this case part of a text string and uppercase. So amazing with a lowercase a isn't a hit.


#9

interestingly, if i change the A into a lower case a in the variable text and variable myName, i get the number of hits equal to 48, which is obviously incorrect, should be 12. But i am assuming it will count all the lower case a's in the text then run the second for loop which then adds to the total number of hits, thus bringing the output to 48 hits. Anyways, thank you for replying.


#10

hey, whats my mistake here:

var myName = "John";

var hits = [];

for(i = 0; i < text.length; i++);
if(text[i] === myName[0]);

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

#11

Hey, where is your var text? you need to declare a variable text and it should have a string input and in that string you should have a 'name' (your name) that you will be searching for. Think about it, your for loop condition i < text.length (where the loop you specify ends) is not even declared anywhere in your code, hence, it won't run. It won;t find the variable: text.


#12

Regarding your topic, one of the mods discusses this loop:


#13

var text = "Hey, how are you doing? My name is John. I am trying to learn how to code, but this is not going very well. As usual Mr. John starts losing interest once he finds something else to do.";

var myName = "John";

var hits = [];

for(i = 0; i < text.length; i++);
if(text[i] === myName[0]);

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

#14

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