My code work but don't pass the validation, why?


#1

Hello, I have this error
Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

I don't understand why, my code seem to work, the array hits get all the iteration of myName

Thank you for your help :smile:

/*jshint multistr:true */

var text = "bla blabla blabla blabla blabla blabla bla Michael\
bla blabla blabla blabla blabla blabla blabla blabla blabla bla\
bla blabla blaMichaelbla blabla blaMichael bla blabla blaMichael\
bla blabla blabla blabla blabla bla Michaelbla blabla blabla bla"
var myName = "Michael"
var hits = []

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

#2

Not Sure what you are looking for but try this and see if it is what you want

var text = "bla blabla blabla blabla blabla blabla bla Michael\
bla blabla blabla blabla blabla blabla blabla blabla blabla bla\
bla blabla blaMichaelbla blabla blaMichael bla blabla blaMichael\
bla blabla blabla blabla blabla bla Michaelbla blabla blabla bla"
var myName = "Michael"
var hits = [];
for (var i = 0; i < text.length; i++) { 
   if (text[i] === myName[0]){
       //what i am doing here is that
       // if you find an 'M', then look ahead
       //exactly 6 characters away from your current position
       //if,together with the M matches my entire name
       // then push to hits
       var found = text[i]+text.substring(i+1, i+myName.length);
       
       //debugging statement
       console.log(found);
       
       if(found === myName)
           hits.push(found);
       }
       
}
console.log(hits);//[ 'Michael', 'Michael', 'Michael', 'Michael', 'Michael' ]
console.log("There were "+ hits.length + " hits of '"+myName+"' found in the given text");

3/7 Your first "for" loop
#4

Thanks you rydan, I copied paste your answer and it worked,
Still I tried to improve mine, it sitll work but don't pass the codeacademy test, I don't know why

The error is :
Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

/*jshint multistr:true */

var text = "bla blabla blaMimibla blabla blabla blabla bla Michael\
bla blabla blabla blabla blaMichbla blabla blabla blabla blabla bla\
bla blaMbla blaMichaelbla blabla blaMichael bla blaMibla blaMichael\
bla blaMichaebla blabla blabla blaMicbla bla Michaelbla blabla blabla bla"
var myName = "Michael"
var found = ""
var hits = []

for(i=0; i <= text.length; i++){
    if(text[i]===myName[0]){
        for(j=0; j< myName.length; j++){
        	found += text[i+j];
        }
        if(found == myName){
        	hits.push(found);
        }
        
        found = "";
            
    }
}
console.log(hits ,hits.length); [ 'Michael', 'Michael', 'Michael', 'Michael', 'Michael' ] 5

#5

try this. Take a look at the second for loop

var text = "bla blabla blaMimibla blabla blabla blabla bla Michael\
bla blabla blabla blabla blaMichbla blabla blabla blabla blabla bla\
bla blaMbla blaMichaelbla blabla blaMichael bla blaMibla blaMichael\
bla blaMichaebla blabla blabla blaMicbla bla Michaelbla blabla blabla bla"
var myName = "Michael"
var found = ""
var hits = []

for(i=0; i <= text.length; i++){
    if(text[i]===myName[0]){
        for(j=i; j< myName.length+i; j++){
            found += text[j];
        }
        if(found == myName){
        	hits.push(found);
        }
        
        found = "";       
    }
}
console.log(hits ,hits.length);

test it here


#6
j=i

thank you, that great !
and thank you for the codecademy labs, I didn't know it :smile:


#7

Happy coding buddy :wink:


#8

Ho, and one thing, how do you color your syntax here on the forum ?
Thanks :smile:


#9

To get the syntax highlighting you have to surroundthe beginning and the end of your code with three bacquotes

`

each