5/5 please help


#1

WHAT AM I DOING WRONG?
it KEEPS saying my second for loop isnt pushing values to the hits array

/*jshint multistr:true */
var text = "yo wassup karim. how're you? k. well i dont really care karim. please just die";
var myName = "karim";
var hits = [];

for(var nameSearch = 0; nameSearch < text.length + 1; nameSearch++) {

if(text[i] ===myName[0]){

 for(var pushCharacter = nameSearch; pushCharacter ===  

 myName.length +1; pushcharacter++){


     hits.push(nameSearch);
 }

}

}


#2

There are two problems in your code.

First one, syntax error -> ReferenceError: i is not defined. Variable i is not defined so you should use something else here:

if(text[i] ===myName[0]){

Second problem is related to the second part (condition) of the inner for loop:

pushCharacter === myName.length +1

Are you sure this is right? This is what the hint says:

Your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable.


#3

var text = "yo wassup karim. how're you? k. well i dont really care karim. please just die";
var myName = "karim";
var hits = [];

for(var nameSearch = 0; nameSearch < text.length + 1; nameSearch++) { // 1
   if(text[i] ===myName[0]) { // 2
      for(var pushCharacter = nameSearch; pushCharacter ===  myName.length +1; pushcharacter++) { // 3, 4, 5
         hits.push(pushCharacter ); // 6
      }
   }
}
// 7

You have 7 errors. I marked lines with a counter where are errors and corrected your code:

var text = "yo wassup karim. how're you? k. well i dont really care karim. please just die";
var myName = "karim";
var hits = [];

for(var nameSearch = 0; nameSearch < text.length; nameSearch++) {
   if(text[nameSearch] === myName[0]) {
      for(var pushCharacter = nameSearch; pushCharacter <  myName.length + nameSearch; pushCharacter++) {
         hits.push(text[pushCharacter]);
      }
   }
}
console.log(hits);

In this post I described how it works: click me.

Why did you use so difficult variable's names in for loops? I think using of var i = 0; ... is more easier for reading and understanding.


#4

It does not make any sense to use == instead of === :slight_smile:


#5

I marked this line because he used undefined variable i. And, of course, in this case it doesn't make sense, I just pushed = twice and didn't check it :slight_smile:
Thanks for your reply! I edited my answer.


#6

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