My Code dosent work (solved)


#1

/*jshint multistr:true */

var text = "abe dasdaf abe asfjdsfs asfdsafds gthergbcv dfsgsdfbvxcb abe abe sdfas abe afjsdgun"
var myName= "abe"
var hits=[]

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

I've been looking at other peoples strings of code and I have tried everything I can imagine to fix this and I still receive the error "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." help would be very greatly appreciated :smile:


#2

never mind solved it haha


#3
var text = "abe dasdaf abe asfjdsfs asfdsafds gthergbcv dfsgsdfbvxcb abe abe sdfas abe afjsdgun"
var myName= "abe"
var hits=[]

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

this is the correct code the problem was that i wasnt looking for the letter in the text so by changing the i[0] to text[i] i was able to solve it if someone could do a better job of explaining what i did it would be much appriciated


#4

You previously had text[0] which told the computer 'if the first letter of text is equal to the first letter of myName(which it was), do the block in the curly brackets'. With the new code, it told the computer to do the block whenever text[i] matched a letter in myName.