5. Your second for loop - isn't pushing values to hits array


#1

Exercise 5 your second for loop

I'm getting 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."

var text = "Hello, what is your name? My name is \
James.";
var myName = "James";
var hits = [];
for(i = 0; i > text.length; i++) {
    if(text[i] === 'J') {
        for(var j = i; j < (myName.length + i); j ++) 
        {
            hits.push('James');
            console.log(hits)
    }
    }}

I cannot find what is wrong someone please help


#2

Problem is in the condition of the outer loop:

i > text.length

, this loop will never be executed. Because initial value of i is 0 and 0 is not greater than text.length. It should be:

i < text.length

In this exercise you should push to the hits array only the actual letter (text[j]).


#3

thanks that worked well for me


#4

You're very welcome :slight_smile:


#5

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