Your second "for" Loop not stopping?


#1

Hey guys everytime I try Saving and submitting my code I get the error "Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length."

My code is below


> /*jshint multistr:true */

> var text = ("I am the king of the unverse!");

> var myName = ("Gavin");

> hits = [];

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

#2

Try:

for (var j = i; j < myName.length + i; j++)

#3

Changed my code to

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

Now I'm getting a different error that says "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."


#4

Make sure it's working properly and that myName's text appears somewhere in the text variable.

Your variable myName doesn't have your name and you put strings inside ( ).

var text = "Gavin is the king of the unverse! Gavin!";
var myName = "Gavin";
var hits = [];
var k = 0; 

for (var i = 0; i < text.length; i++){
    if (text[i] === myName[0]){
        for(var j = i; j < myName.length + i; j++){
            hits.push(text[j]);
        }
        ++k;
    }    
}
if (hits.length <= 0) {
    console.log("Your name wasn't found!");
}
else {
    var times = "";
    if(k === 1)
       times = "time";
   else if (k > 1)
       times = "times";
    
   console.log("Your name was found " + k + " " + times + "!");
}

#5

Oh! I see it now, thank you so much! I appreciate it :slight_smile:


#6

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