Getting unexpected token } error


#1

Okay guys, here is my code:
/*jshint multistr:true */

var text = "Ladies and gentlemen, that dude in a tree, getting coded\
by a large degree of Brodie."

var myName= "Brodie"

var hits =[]

for (var i =0; i < text.length; i++) {
if (text[i] === 'B') {
for (var j = i; j > i + myName.length; j++)
}
}
}
I keep getting an error of "unexpected token }, but when I gradually delete them until none remain it turns into unexpected end of input. Help??


#2

Start by counting the opening braces (LBRACE) {. Note the number. Now count the closing braces (RBRACE) }. The number should match.

Next look for pairing and nesting. To do this, start with the deepest nest. There should be both a { and a } wrapping the inner code. Now as you back out of the nest, that code should be wrapped, and so on to the outermost.

These two steps will help you solve the above. I also see there is some missing code. This will help pinpoint where.


#3

Thank you! Your comment helped tremendously, but now I have a new error! (A common one it seems). My code is not pushing the array value. Here is my new code(Just from the For loop onwards):

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


#4

@demonickaze,

As automation is the trial of creating a copy of the reality....

Imagine you have a piece of text

You will go over this text with your left-index-finger
and will do it character for character untill the End-of-Text
and you keep =count= by using leftIndexFingerPos

for (var leftIndexFingerPos = 0; 
         leftIndexFingerPos < text.length; 
         leftIndexFingerPos++) {

At every leftIndexFingerPos you =indentify= the current character
through text[ leftIndexFingerPos ]
and want to compare this current-character with "J"

for (var leftIndexFingerPos = 0; 
         leftIndexFingerPos < text.length; 
         leftIndexFingerPos++) {

    if ( text[leftIndexFingerPos] === 'J') {
        //IF codeblock will only be entered 
        // if text[ leftIndexFingerPos ] equals "J"
    }
}

Now if you identified the current-character
being at leftIndexFingerPos as being a "J"
you place your =rightIndexFinger= on-top of your =leftIndexFinger=

you now have the rightIndexFingerPos at the position of the leftIndexFingerPos
var rightIndexFingerPos = leftIndexFingerPos;

Now you are going over the text with your =rightIndexFinger=
and do that as long as the condition
rightIndexFingerPos is less than (leftIndexFingerPos + myName.length)
rightIndexFingerPos < (leftIndexFingerPos + myName.length);

They want you to insert
every character found at rightIndexFingerPos
into the hits Array using the push()_ Method
thus
every-character-found will be a =new Array Element=

for (var leftIndexFingerPos = 0; 
         leftIndexFingerPos < text.length; 
         leftIndexFingerPos++) {

    if ( text[leftIndexFingerPos] === 'J') {
        //IF codeblock will only be entered 
        // if text[ leftIndexFingerPos ] equals "J"
        for (var rightIndexFingerPos = leftIndexFingerPos;
                 rightIndexFingerPos < (leftIndexFingerPos + myName.length);
                 rightIndexFingerPos++) {
             hits.push( text[rightIndexFingerPos] );
             //internal increment of rightIndexFingerPos with 1, and check condition 2nd FOR-loop
        } //end-of-2nd-FOR-loop
    } //end-of-IF-statement
    //continue read starting with leftIndexFingerPos + 1
} //end-of-1st-FOR-loop
console.log( hits );

Reference

== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push


#5

This is what @leonhard.wettengmx.n is referring to. j cannot be both equal to i and greater than i + myName.length. Change the sign to < as suggested and all will be well. And you are right. This is a common error, but happily not often repeated once learned about. Whew!

One more thing. The program is expected to append only one letter at a time. Be sure to push just a letter at index j of text.