Your second "for" loop


#1


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


Oops, try again.
Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.


/*jshint multistr:true */
var text = "Corban, Corban, Corban";
var myName = "Corban"
var hits = [];

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


#2

var myName = "Corban" <-----Missing semicolon


#3

I'm running into the same problem and tried to solve the problem the same way. The 2nd loop is not comparing j to the length of your name. So change myName to myName.length. And, I believe inside the push parenthesis you need to tell the script what to add.

However, even doing that you'll still get the same error. The code below outputs an array and i believe should be correct. However, it is not correct to codeacademy's checking.

var text = "Anthony ajsndkjanskdj \ Anthony Anskdnakjn Anthony jandjn asjdklas \ asjdfkasd ju Anthony";
var myName = "Anthony";
var hits = [];

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

console.log(hits);


#4

Remove all three semicolons after the curly braces };<--

and you can't do this

"Anthony ajsndkjanskdj \ Anthony Anskdnakjn Anthony jandjn asjdklas \ asjdfkasd ju Anthony"

putting slashes is called escaping in this example so you have to remove those


#5

Thanks for the suggestions. I left the backslash in from the previous lesson that explained it as a way to wrap the text. I left out the commented line that the prompt used and included it below.

The problem was that my first for loop was allowing the second for loop to try to pull characters from beyond the length of the text variable. I adjusted this to stop scanning the text when it reaches the text.Length - myName.Length + 1. The 1 is to include the final position where my name could possibly located.

/*jshint multistr:true */

var text = "Anthony ajsndkjanskdj \ Anthony Anskdnakjn Anthony jandjn asjdklas \ asjdfkasd ju Anthony";
var myName = "Anthony";
var hits = [];

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

console.log(hits);

#6

this is how the code is suppose to be

/*jshint multistr:true */
var text = "blah blah blah blah blah \
Sam blah blah blah blah Sam";
var myName = "Sam";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "S") {
for (var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}


#7

/*jshint multistr:true */
var text = "blah blah blah blah blah \
Sam blah blah blah blah Sam";
var myName = "Sam";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "S") {
for (var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}


#8

why (myName.length + i)?


#9

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