Wrong code?


#1

I am almost done with the "Search text for your name section, but I ran into a problem. whenever I run this code this comes up.

'Oops, try again. 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.'

Could someone possibly tell me where I may have messed up the code?

var text =('Clyde hello hello hello / hello hello hello yo yo yo mate Clyde whats up mate')
var myName = 'Clyde'
var hits = []
for ( var i = 0; i < text.length; i ++);
{
    if (text[i] === 'C') { 
        console.log
       for (var j = i; j < (myName.length + i); j++) { 
           hits.push(text[C]);
       }
   
    }
}


#2

Instead of writing it manually 'C' you can put myName[0] which will give us 1. letter of our name.

This doesn't give you anything. You are pushing some variable called C that is not declared nowhere and it is not even a string because you did not put it inside the " ".
Your second for loop should be pushing text[j] instead of text[C]


#3

Okay, so when I do that, the console logs '5' and it says
"Oops, try again. Make sure you're pushing letters to the hits array!"


#4

Remove the console.log that doesn't do anything.
Remove ; from beginning of for loop and leave only {

p.s. It is good practice to place ; at the end of JS code because syntax error will bother you much more in future if you don't get that habit now.

By the way, just so you know in the future >> if statement syntax is written as:

if(/*something*/){
  //do something
}

#5

Alright, so when I did that it worked, but the console logged
"[ undefined, undefined, undefined, undefined, undefined ]" when I did console.log(hits) at the end


#6

Why did you declare your variable using ( ) ??

Should be var text = "your text here" ;


#7

That doesn't matter. Look at my example and console.log output.


#8

This is the example of your code I gave you before. You see the output is right.

You're not consoling out the right thing but you are on the right way. Your code is outputting array with things in it which are undefined. So it means you should probably check part of the code where you values are being pushed into hints array.


#9

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