I have been working on final code for "For Loops" section 5


#1


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


Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.


/*jshint multistr:true */
var text = "Space", "Time", "Brett", "Final", "Fronteer", "Brett", "First";
var myName = "Brett";
var hits = [];
for  (var i = 0; i < text.length; i++) {
    if (text[i] === "B") {
        for (var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
            console.log("j"+ j);
        }
    }
}


#2

here:

var text = "Space", "Time", "Brett", "Final", "Fronteer", "Brett", "First";

this is invalid, just make text a string


#3

ar text = ("Space", "Time", "Brett", "Final", "Fronteer", "Brett", "First");
var myName = "Brett";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "B") {
for (var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
console.log("j"+ j);
}
}
}
//ok, that should change that to be correct, right?


#4

I think my console.log may also be incorrect but I am not sure.


#5

look:

var text = ("Space", "Time", "Brett", "Final", "Fronteer", "Brett", "First");
console.log(text)

your string now only contains First, so your name Brett can't be found. Just make a string which contains a sentence:

var example = "i am a string and a sentence"

#6

Hello,

As the moderator mentioned, this only needs to be a long text string. Something like


var text = "word1 word2 word3 word4 word5";

Try it this way, and it should work.


#7

Yes, that worked, thank you so much!
and the correct answer should be
[ 'B' ]
[ 'B', 'r' ]
[ 'B', 'r', 'e' ]
[ 'B', 'r', 'e', 't' ]
[ 'B', 'r', 'e', 't', 't' ]
[ 'B', 'r', 'e', 't', 't', 'B' ]
[ 'B', 'r', 'e', 't', 't', 'B', 'r' ]
[ 'B', 'r', 'e', 't', 't', 'B', 'r', 'e' ]
[ 'B', 'r', 'e', 't', 't', 'B', 'r', 'e', 't' ]
[ 'B', 'r', 'e', 't', 't', 'B', 'r', 'e', 't', 't' ] ?


#8

the code is your answer, that is indeed the correct output. Although this exercise does not focus on output yet, that will come in next exercise


#9

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