Issue with the second "for" loop


#1


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


This is my code. I honestly can say that i do not know what I am doing whatsoever. So if you could please explain my errors, that'd be greatly appreciated.


/*jshint multistr:true */

var text = "hskjfsbd uni sdhvjyes jsga sdvghevys uni shgdvtva afd hadddbjusnasi";
var myName = "uni";
var hits = [];
hits.push("uni");
hits[0];

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


#2

Missing j++ in for loop and curly braces are also missing

Correct your code to

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

Also, in 1st for Loop, change it to

for (var i = 0; i < text.length; i++)

Full Code

var text = "hskjfsbd uni sdhvjyes jsga sdvghevys uni shgdvtva afd hadddbjusnasi";
var myName = "uni";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === "u") 
        {
        for (var j = i; j < (i + myName.length); j++) {
         hits.push(text[j]);
        }// end of j for loop
        }//end of if
};// end of i for loop

#3

@hong817 I did not have this:

outside of the for loop.

also I wrote it(it being this: hits.push("uni");) like this:

hits.push(text[j]);

I did not have this line:

You also need another curly-brace at the end

hope this helps

Steven


#4

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