6. Log it


#1

Hi guys!
I really don't know what is wrong with my code O.o
Any help would be appreciate. Thanks!

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

This is my code:

/*jshint multistr:true */

var text = "Rosalia is learning \
abcd Rosalia, \
checking Rosalia";
var myName = "Rosalia";
var hits = [];
for (var i = 0; i<text.length; i++){
if(text[i] === myName[0]){
for (var j = i; j< i+myName.length; j++){
hits.push(text[j]);
}
}

}
if(hits.lenght === 0){
console.log("Your name wasn't found!");
}
else{
console.log(hits);
}

And I get this:

[ 'R',
'o',
's',
'a',
'l',
'i',
'a',
'R',
'o',
's',
'a',
'l',
'i',
'a',
'R',
'o',
's',
'a',
'l',
'i',
'a' ]


#2

Start by determining what's wrong about its behaviour. Then you can start considering why it wouldn't do that/what could possibly be responsible. Once you have your suspects you can start making observations about how your code runs by adding prints


#3

I was going to point this out because of the spelling of length.

Your 2nd loop collected all the characters from the myName variable "Rosalia". "Rosalia" appears 3 times in your text string. That was logged to the console by the hits array. Notice the [ and ] at the beginning and end of your printed results.


#4

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