Second for loop...browser crashing...HELP


#1


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


Help please I am really stuck here, my browser keeps on crashing each time I run my code


/*jshint multistr:true */
var text = "glenn kljljklkj glenn  oiuouoiuu"; 
var myName = "glenn";
hits = [];

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


#2

the condition in your second loop:

i + myName.length

there is no comparison, so there is no reason for the loop to stop. What do you think the comparison needs to be?


#3

Oh I see, cannot believe I missed that, I added in the comparison and the other bits and it passed

/*jshint multistr:true */
var text = "glenn kljljklkj glenn oiuouoiuu";
var myName = "glenn";
hits = [];

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

}

}


#4

Actually not that code, this is the code that works, I was messing about with the above code to fine tune it but it did not work

/*jshint multistr:true */
var text = "glenn kljljklkj glenn oiuouoiuu";
var myName = "glenn";
hits = [];

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

}

}

if (hits.length === 0 ){
console.log("Your name wasn't found!")
} else {
console.log(hits)
}


#5

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