Numbers output


#1


Good evening,
If somebody would be so kind and tell me what I did wrong. Please don't swear on me, everybody can make mistake. :slight_smile:


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661


It gave me badges and let me go to next exercise but I wanna know why in output i got numbers and not letters.


var text =  "zed, neo";
var myName = "neo";
var hits = [];

for (i = 0; i < text.length; i++) { 
if (text[i] ==="n") {
    for (var j = i; j < i + myName.length; j++) {
        hits.push (i);
        }    
    }
}
    if (hits.length === 0) { 
        console.log("Your name, wasn't found!")
    }
   else {
   console.log(hits)
   };


#2

that would suggest you push the wrong thing into the array:

hits.push (i);

why did you choice to push i into the array?


#3

Because for j was in if i, just as child to daddy


#4

sorry, what do you mean?


#5

for loop is inside if statement, that's why I choose it


#6

but you use the second for loop (j) to find your name in text, so then j contains the indexes of your name in text, so then text[j] provides with the letters (using square brackets to access string by index)


#7

[ 'n', 'e', 'o' ]
got it, Thank you!


#8