Your second "for" loop


#1

I think I did the steps correctly but there is an error I still get for not completing an action.
Code:

/*jshint multistr:true */
var text = "Hey how are you doing?\
my name is Coolboy43" ;
var myName = "Coolboy43" ;
var hits = [] ;
for(var i = 0; i < text.length; i++); 
if (text[i] === 'C') {
    for(var j = i; j > i+myName.length; j++);
    hits.push(text[j]);
}

Error:

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.

I know it's saying me to print myName somewhere, but if I use console.log it doesn't work!


#2

for/loop syntax..

for (first part; second part;third part) {
  //your code
}

One more thing, the body of for loop starts with { and ends with }.
same true for conditional clause like if too.

Use these two points and try to fix the code, if you get confusion, point them,I will explain.


#3

I fixed the two points. All I did was add some curly brackets though.

Now to the printing part, I just don't get what it means by that.


#4

Can you put your updated code?


#5

Sure.

/*jshint multistr:true */
var text = "Hey how are you doing?\
my name is Coolboy43" ;
var myName = "Coolboy43" ;
var hits = [] ;
for(var i = 0; i < text.length; i++); { 
if (text[i] === 'C') {
    for(var j = i; j > i+myName.length; j++); {
    hits.push(text[j]);
        }
    }
}

#6

Check the condition of second loop. Do we need to make any change(s) there ? :slight_smile:

(also we need to erase the ; 's after end of each of the for loop headers)
Check the syntax of for loop!


#7

j < i+myName

I think that's one.
i+myName should be (myName.length + i)

Those are the only ones I can find for now ;-;


#8

Yes, the condition! Now we are close, does code ran , if not? then take a look these pics.
This standard syntax of for loop.

I have pointed them,, that makes your code not work properly!


#9

You're right! I have used the semi-colons in the past too but they haven't shown any problems then. That's why I thought adding them now would just be natural. Thank you for your help! :relaxed:


#10

Always look at syntax when in doubt, helps a lot. :slight_smile:
Semicolons basically goes after a statement, and it acts as "end point".
It (Semicolons) is going to come a lot in future (exercises) so its important to take a look back and revise. :wink:


#11

Ah I see. I have to keep that in mind while learning more. Thanks again!


#12

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