5. Your second "for" loop


#1

/*jshint multistr:true */
var text = "Hello, Winter. I am Winter's computer.";
var myName = "Winter";
var hits = [];
for(var i= 0; i > text.length; i++)
{ if( text[i] === 'W')
{ for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
};
}
}

That is my code. "Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable." Keeps popping up, and I cna't figure out what is wrong. :Ppensive:


#2

Change the condition ot the first loop to:

i < text.length

#3

/*jshint multistr:true */
var text ="When I was young my name is Nima and\
true Nima because its new Nima\
perfect Nima.";
var myName = "Nima";
var hits = [];

for(var i=0; i{
if(text[i]==="N"){
for(var j=i; j {

   }

}
}
whats's wrong?


#4

I'm having the same issue. Grrrr.


#5

try this .

Edit:

The excerpt is now complete with comments and explanations of each iteration

var text = "When I was young my name is Nima and\
true Nima Nbecause its new Nima\
perfect Nima.";
var myName = "Nima";
var hits = [];

for(var i=0; i<text.length;i++){
    //lets make it more dynamic and
    //compare it like this so then when the
    //value of myName changes we dont have to change
    //anything
    if(text[i]===myName[0]){
        
        //create a variable to store subsequent substrings
        found = "";
        
        /*this loop begins when the current character
        matches the first letter in myName. the loop will
        start at the index where i got the match(i.e.i) and will
        only run for the length of myName.
        
        Now we write this statement j<myName.length+i adding the
        'i' to ensure that we always begin the second for loop at the 
        index where we found the match
        
        */
        console.log("Found '"+myName[0]+"'. Making iterations to check for a hit");
        for(var j=i; j<myName.length+i;j++) {
            
            //as we iterate the text we are capturing
            //all the characters and adding it to found
            found+=text[j];
            
            //debugging statement
            console.log(found);
        }
        // each time the second for loop is done the compiler
        //comes down here and will check if the string captured in 
        //found so far matches myName entirely
        //if it does then add it to hits
        //else do nothing with it
        
        if (found===myName) {hits.push(found);}
        //execution trace statement
        console.log(found === myName?"Found a hit ==>> "+ found:found + " was not a hit");
        console.log("\n");
    
    }
}

console.log(hits);

Click here to test it on Labs


I have no clue what to do for this step, please simplify what to do
Exercises Three through Six are not working
#6

The error message I'm getting is this:

Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

And here's my code:

/*jshint multistr:true */

var text = "Human beings are wonderful, Jesse! \ You should get to know \ a few of them. You might find \ other Jesse people around here. \ Look closely, especially for \ cool kids. They're probably named \ Jesse.";

var myName = "Jesse";

var hits = [];

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

#7

You put a semi colon before your first corresponding bracket and after your for loop:


#8

on some of these:

if(text[i] === "J") // you added myName[0]
for(i=0; i < text.length; i++) // you added a semi-colon

oh , and when ending the script, only use " }; " once, you've used it 3 times at the end.
// don't forget to print out at the end!


#9

I would suggest not to just say the answer... we have to help and let them understand the problem syntax....:smiley:

Please do not just say the answer, for people can get the same problem over and over again.... and that isn't good. So please do not just say the answer, unless it's for a problem in syntax.


#10

Thank you so much! I got so used to putting semicolons after parentheses, I guess. So NO SEMICOLONS after conditional statements. Got it! ^_^


#11

sorry about that, first time attempting to help out on something I myself was recently stuck on, won't happen again :+1:


#12

Made a edit to my original post in this thread