5. Your second "for" loop


#1


SyntaxError: missing after for-loop condition


var text = "My name is James! My friends call me James.";

var myName = "James";

var hits = [];

for (i = 0; i < text.length; i++) {
     if (text[i] === 'J') {
         for (var j = i; j < (myName.length + i), j++) {
              if (text[j] === myName[j-i]) {
                   hits.push(text[j]);
              }
         }
     }
};
if (hits.length === 0) {
     console.log("Your name wasn't found!");
          } else {
               console.log(hits);
}


#2

Got a question?


#3

Yes. What am I doing wrong?


#4

According to the error message you posted, there's a syntax error in your code!
Got another, perhaps something you were really wondering? Cause if I'm both asking and answering then I will be talking to myself


#5

Where could the missing " " be?


#6

Closer. Still rephrasing that to "How can I locate this syntax error?" Because knowing only the location of the syntax error in that particular piece of code isn't worth much.
Two answers:
1) remove code and paste it back until the syntax error is back
2) run the code in another interpreter, one that gives a line number for errors. Your browser has a js interpreter (that's why you would learn js). Some browsers let you run snippets, such as firefox with shiftf4 or chrome in the dev console under snippets. There are also online sites that expose it or run it on their servers. Or nodejs/others.. take your pick


#7

Like cut one line at a time or a couple? Or does that matter?


#8

The most efficient way to do it would be with binary search, same as you would do with finding a word in a dictionary. You start in the middle and then repeatedly move to the middle of the half you have determined it's in. Can't just cut it anywhere though, has to be correct syntax each time you run something.

That's how you can find it manually, but running it in another interpreter will give you a line number, far more efficient.


#9

Thanks! I'll give that a try right now.


#10

for (var j = i; j < (myName.length + i); j++)

This is the part you are getting wrong.


#11

shall i know, how this condition become equal...?
if ( text[i] === 'J')
here text[i] is a string and 'j' is a single word
how can we compare this??


#12

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