Your second “for” loop | It looks like your second ‘for’ loop isn’t pushing values to the hits array


#1


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


It looks like your second ‘for’ loop isn’t pushing values to the hits array


var text = "Josh name name name Josh";

var myName = "Josh";

var hits = [];

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


#2

I'm not sure what this 1st loop does. If i is assigned 0, why set i to the exact value of the text.length and then add 1 to i afterwards? Change the comparison operator to the less than sign to make your loop check the characters in your text string one by one. Do the same for your 2nd loop.


#3

A post was split to a new topic: What's wrong with my code?


#5

I don't see that happening in @devuniverse's code. Give your explanation another go.


#6

I phrased this terribly :laughing:

If i is assigned 0 and the condition is i === text.length, the loop won't run because the text.length is 24. The comparison operator needs to be adjusted in order for the loop to run.


#7

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