Code accepted but wrongly coded?


#1


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


when i press "Save and Submit Code", it accepts my code but it only displays the number 21. Did i program it wrong, or is the code correct and do i need to have another look at how "for" loops work?


var text = "Patrick did this, Coca cola Patrick is, better than pepsi Patrick";
var myName = "Patrick";
var hits = [];

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


#2

what did you console?
you should write

console.log(hits);


#3

Thank you rcodeman for responding

I guess it's time for me to stop and get some sleep for making such a mistake :slight_smile:
i added console.log(hits); at line 14 and it displayed this answer:

[ 'P',
'a',
't',
'r',
'i',
'c',
'k',
'P',
'a',
't',
'r',
'i',
'c',
'k',
'P',
'a',
't',
'r',
'i',
'c',
'k' ]

Is this correct? Because i want to make sure my code is correct before i move on to the next part.


#4

yes.it is correct. :slight_smile:


#5

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