Your Second for loop...very confused on loop syntax


#1

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

I have been getting error messages such as "unexpected end of input", something about the "left side".
Here is what I have, and I already got a bit of help with other forum posts. I'm justnot sure of my syntax and how I am even supposed to go about setting the for loop up to add the other letters of my name to the array. Also since when are we using the variable 'j'?? Hah.

var text = "Lorem ipsum dolor sit amet, consectetur Bethany adipiscing elit. Vestibulum Bethany lacinia ipsum quis orci aliquam, eget viverra justo imperdiet. Quisque ultrices dolor nibh, id mollis odio aliquam Bethany vitae. Cras egestas laoreet est, Bethany vitae feugiat risus convallis eget. Praesent non ornare est, ut suscipit massa. Etiam imperdiet Bethany luctus placerat.";
var myName = "Bethany";
var Hits = [];

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

Any and all help appreciated ...I'm brand new to coding. THANK YOU!


#2

Hi, I am only an amateur coder myself, but i believe the reason may be due to the fact you have not inserted your last ' } '. I have tried this with your code and it worked for me; so simply add a } onto the end.

var text = "Lorem ipsum dolor sit amet, consectetur Bethany adipiscing elit. Vestibulum Bethany lacinia ipsum quis orci aliquam, eget viverra justo imperdiet. Quisque ultrices dolor nibh, id mollis odio aliquam Bethany vitae. Cras egestas laoreet est, Bethany vitae feugiat risus convallis eget. Praesent non ornare est, ut suscipit massa. Etiam imperdiet Bethany luctus placerat.";
var myName = "Bethany";
var Hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === ["B"]) {
        for (var j = i; j < myName.length + i; j++) {
           if(text[j] === myName[j-i]){
              hits.push(text[j]);
           }
        }
    }
**}**

#3

You're missing one closing }. You have also called your var 'Hits' with a capital H and in your if statement you refer to 'hits', without the capital H. If you change this it should work.


#4

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