5/7 it passed a month and i can't resolve it!


#1

can someone help me???
var text="gale likes to talk about she";
var myName="gale";
var hits = [];
for(var i = 0; i > text.length; i ++) {
if (text[i] === "g"){
for(var j = i; j < (myName.length+i); i ++) {
hits.push(text[j]);
}
}
}
every time i see this problem it makes my head hurt!


#2

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

You're already incrementing your " i ", so instead increment your " j ".

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

#3

uhm.. @lolman, you missed something:

for(var i = 0; i > text.length; i ++) {

i > text.length, will you do the honor lolman of explaining it?


#4

OOOO, Got me coach,

for(var i = 0; i > text.length; i ++) {
                 ^

Essentially you're saying as long as " i " is greater than the variable "text"s length, this is essentially saying this for loop will never run because the condition has already been met.

This is a Logic Error, and not a Syntax Error, meaning the console won't error out, however, you will not be capable of having the correct information logged to the console in latter lessons.

Just flip that "greater than" to a "less than" sign and you will be good to go!


#5

still error. i changed to less than and it made and infinite loop! (the first one)
and if i change the two it says "oops try again" :frowning:

var text="gale likes to talk about she";
var myName="gale";
var hits = [];
for(var i = 0; i < text.length; i ++) {
if (text[i] === "g"){
    for(var j = i; j < (myName.length+j); i ++) {
     hits.push(text[j]); 
    }    
}
}

here makes an infinite loop
and

var text="gale likes to talk about she";
var myName="gale";
var hits = [];
for(var i = 0; i < text.length; i ++) {
if (text[i] === "g"){
    for(var j = i; j > (myName.length+j); i ++) {
     hits.push(text[j]); 
    }    
}
}

here says "oops try again" >:o


#6

this for loop construction is wrong:

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

our first hit is at index 0 (text[0]). So, i = 0. Now we are going to enter the loop. j = i, so j = 0 as well. then if j (0) < (myName.length+j) (4); i++

So j will stay zero, and never be bigger then four (infinity loop).

You should do j++ instead of i++, you want the loop to end when all hits are pushed