Lesson: Search Text for your name...Your second "for" loop


#1


Look, I understand this code probably looks all wrong. Don't bash me, just help.

There was no error message. The code would crash my browser. I can't, for some reason, figure out how to stop the code from being an infinite loop.


var text = "Kyle Kyle Kyle";

var myName = "Kyle";

var hits = [];

for(var i = 0; i < text.length; i++) {
    if (text[i] === 'K') {
       for(var j = i; i + myName.length; i+= 1) {
          newArray =[];
          newArray.push('Hello');
          newArray[0];
          
       }
           
    }
}


#2

the condition of your second for loop:

i + myName.length

it is not really a condition, you want to loop from i till i +myName.length with j, so maybe you should check if j is smaller then (<) i + myName.length?

also, since you are looping j, increase j, not i

also, you already defined the hits array outside the loop (good thing, otherwise it would reset every time), just push text[j] into the hits array. j contains the indexes of your name, so you can use this indexes to get your name from text


#3

Now there's and actual error message, a syntax error

var text = "Kyle Kyle Kyle";

var myName = "Kyle";

var hits = [];

for(var i = 0; i < text.length; i++) {
if (text[i] === 'K') {
for(var j < i; j + myName.length; i+= 1) {
.push(text [j]);
newArray =[];
newArray.push('Hello');
newArray[0];

   }           
}

}


#4

yea, the initialization of your second for loop was fine: j = i, i was talking about the condition which needed upgrading: j < i + myName.length


#5

It gives me a syntax error that I have an empty string ' '/" "

var text = "Kyle Kyle Kyle";

var myName = "Kyle";

var hits = [];

for(var i = 0; i < text.length; i++) {
if (text[i] === 'K') {
for(var j = i; j < i + myName.length; i+= 1) {
.push(text [j]);
newArray =[];
newArray.push('Hello');
newArray[0];

   }           
}

}


#6

you should push text[j] into the the hits array.

all this code:

newArray =[];
          newArray.push('Hello');
          newArray[0];

is redundant. you still increase i instead of j in the second for loop.

You only seem to have done half my instructions, my instructions are to help you, not to code everything for you


#7

I am receiving a similar message: "Oops, Try again. Careful: Your second 'for' loop should stop when it reaches its current point in the string + myName.length. " I read the comments and tried several things but nothing seems to be working.

Here's my code:

var text = "Melinda Melinda Melinda";
var myName = "Melinda";
var hits =[];

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


#8

i don't see current point reflected in your code? i contains the first letter of your name, so j needs to loop from i till i + myName.length


#9

OK...I must be really tired because I'm not quite understanding what you mean....

Is it this?
j < i (i+myName.length)


#10

yes it is, given i contains the first letter of your name (thanks to your if condition, so then you let j loop from till i + myName.length since that is where you name is.


#11

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

Now I receive "TypeError: i is not a function"


#12

NVM....figured it out!

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

#13

that still looks like a bad idea, look:

for (var j = i; j< (i+ myName.length); j++) {
   console.log(j)
}

so j contain the indexes which happen to match with your name, we can see this:

for (var j = i; j< (i+ myName.length); j++) {
   console.log(text[j])
}

so we want to push text[j] into our hits array, since the purpose of this program is to find the letters of your name in a string.


#14

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