Second for loop



Codeacademy says I pass the lesson but my code doesn't return my name. It returns a number.

var text = " bbbbbbbbbbbb bbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbb jake vvvvvvvvvvvvvvvvvvvvvvvv vbvvvvvvvvvvvvvvvvvvvv JAKE JAKE";
var myName = "jake";
var hits =[];
for ( i=0;i<text.length;i++){
if (text[i] ==="J","j") {
for (var j=i;(j<myName.length+i);j++) {
if (hits.length=0) {
console.log ("your name wasn't found");
else {
console.log (hits);

Can someone help?


This line is the problem,

Your pushing nothing to the empty variable hits. In parentheses you need to include the first for loops instant variable and also the init j of the second for loop.



I am wondering how this line works.


There must be enough required elements to satisfy the lesson check (SCT). However, the code contains a number of errors.

Not really an error, but would you want your teacher to write your name like this? In grammar we call it a proper name, which deserves to be capitalized.

var myName =  "Jake";

Also not really an error, however it is a best practice to declare the iterator variable inside the loop parameters if it is not already declared elsewhere.

for (var i = 0; ...

The interpreter does not expect , "j". This is not how we compare multiple values.

if (A === "J" || A === "j")    // note two unique expression operands

However, this is not a concern. In the text J used, so we need only look for that match.

if (text[i] === "J")

and since we have capitalized the name,

if (text[i] === myName[0])

Nothing is being appended to the array. We need to pass a value...


The above is an assignment in a conditional which is invalid. It must be a comparison.

if (hits.length === 0)

Finally, on another point of grammar, Sentences start with a capital...

"Your name wasn't found."


Thanks for the replies!

This is all really helpful.

I don't understand why I need to add [i] in the line j < myName.length + [i].

Thanks guys


We add i because we need the offset. The matched letter is found at position i. That is where j begins iteration of text.


will be at j = i


will be at j = i + 1


will be at j = i + 2. And so on...


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