5 - Second for loop - I passed but don't understand why


Here is my code which passed but I don't understand why. It printed out '27'. I thought the computer was to search the thread and add all instances of my name to the array. Don't know what I'm supposed to have taken from this lesson.

/*jshint multistr:true */

var text = "blab Darkhorse blab Darkhorse blah Darkhorse";
var myName = "Darkhorse";
var hits = [];

for (var i=0; i < text.length ; i++){
if (text[i] === "D"){
for (var l=i; l< i+ myName.length; l++)



Same here! It doesn't make sense to me.


I figured it out! I was having the same trouble but what it's doing is counting each words length and adding it together. For example it says Darkhorse 3 times and Darkhorse's length is 9. So 9 * 3 equals 27.


Thanks for that.
I still thought that push was to 'push' the words into the array as it does in Python. Seems weird to me especially as there are so many other language similarities. Guess I'll work it out as we go along. :smile:


mine counted the number of times my name appeared correctly but just like you said there were no wors pushed into the array. Have you found out if its just like that ?


idk either. i have this:
var text = "once upona time autocorrect never existed jose was vrry happu in this land. ot waz a land with jose typinh hid name poinmcorrectlu.";
var myName = "jose";
var hits = [];
for(var i = 0; i < text.length; i+= 1) {
if (text[i] === 'E'){



I've also passed this one but struggling to understand why, here is my code:

/*jshint multistr:true */
var text = "I am Natalie and I'm trying to learn how to\ code. This is quite a struggle for Natalie!";
var myName = "Natalie";
var hits = []

for(var i=0 ; i<text.length ; i++) {
if (text[i] === 'N') {
for(var j=i ; j <(myName.length + 1) ; j++) {


I've tried to explain it to myself in writing which I find easier, is the below a correct explanation??

The second for loop needs to link to the first one, hence creating variable j and then = it to i. j then needs to be less than my names length (7) + one. It continues this loop until its found a word starting with N that IS NOT less than my names length. That's what ends the loop, when it becomes incorrect. So then the hits.push function activates the array, which finds 3 instances of my name, producing the number 3 in the console.


Hi Natalie,
I have also passed but I'm not really sure who's 100% correct and why!
This is my code. I ckecked several ones including your code before I got this result.

CAN SOMEONE present the FULL 100% CORRECT CODE for this exercise? I think mine isn't bad but I'm not sure anymore. Very confused at this point!



understood the moment i read it


/*jshint multistr:true*/
var text = "Jake learning JS. Jake NOT stuck.";
var myName = "Jake";
var hits = [];
for(var i = 0; i < text.length; i++){
if(text[i] === 'J') {
for(var j = i; j < myName.length + i; j++){


Result should be:

[ 'J', 'a', 'k', 'e', 'J', 'S', '.', ' ', 'J', 'a', 'k', 'e' ]