Output is the number 12 and not my name


#1



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


The code will run but, the output I am receiving is the number 12 instead of the letters of my name.


/*jshint multistr:true */

var text = "Mike, Lynn, Charlie, \
Mike, Lynn, Charlie, \
Mike, Lynn, Charlie"

var myName = "Mike"

var hits = []

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



#2

Hi if you want to print your name just add this line

console.log(hits)

#3

Hi
I've got the same output in the console -- "12". Adding console.log(hits) as suggested above, will give a repetitive output of my name. In the example of lesson 1, there is no console.log(hits) used either?

Below my code.
/*jshint multistr:true */

text = "He - ho, let's go \
that's what Theo can say. But he \
does not know my name - Denise. \
Denise he does not say.";

var myName = "Denise";
var hits = [];

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


#4

HI Sorry the console.log(hits) should be outside the for loop like that

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

        }
    }
}
console.log(hits);

#5

aargh. Now i feel silly :blush:
clear now and i also see the line in the original example in lesson 1.

Great! Thx for your response!