Log It! problem - the code is accepted but doesn't give me the right result


#1



My code is accepted and codecademy says its correct, but it returns the entire 'text', with each letter in an individual line. Please help me so it will only return my name.


/*jshint multistr:true */

var text = "this is really a really long string \
of text but it doesnt matter because Aakriti its really \
just text. i mean, i don't really know Aakriti what this is \
but it doesnt need to mean anything";
var myName =  "Aakriti";
var hits = [];

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

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


#2

Change var j = 1 to var j = i. What it seems like your 2nd loop is currently doing is pushing all of the characters from the text string starting at 1 (which corresponds to the h in this) and ending at the last letter of the myName variable "Aakriti" into your hits array.


#3

Thank you @biff75, it works perfectly now! :smiley:


#4

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