No errors, but print out is absurd


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661


Accepts my code without errors; however, it prints our the following which I am quite sure is incorrect:
[ 'J',
'o',
'n',
'a',
't',
'h',
'a',
'n',
'o',
'n',
'a',
't',
'h',
'a',
'n',
' ',
'n',
'a',
't',
'h',
'a',
'n',
' ',
'i',
'a',
't',
'h',
'a',
'n',
' ',
'i',
's',
't',
'h',
'a',
'n',
' ',
'i',
's',
' ',
'h',
'a',
'n',
' ',
'i',
's',
' ',
'r',
'a',
'n',
' ',
'i',
's',
' ',
'r',
'e',
'n',
' ',
'i',
's',
' ',
'r',
'e',
'a',
' ',
'i',
's',
' ',
'r',
'e',
'a',
'l',
'i',
's',
' ',
'r',
'e',
'a',
'l',
'l',
's',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
' ',
'e',
'a',
'l',
'l',
'y',
' ',
' ',
'g',
'a',
'l',
'l',
'y',
' ',
' ',
'g',
'o',
'l',
'l',
'y',
' ',
' ',
'g',
'o',
'o',
'l',
'y',
' ',
' ',
'g',
'o',
'o',
'd',
'y',
' ',
' ',
'g',
'o',
'o',
'd',
' ',
' ',
' ',
'g',
'o',
'o',
'd',
' ',
'a',
' ',
'g',
'o',
'o',
'd',
' ',
'a',
't',
'g',
'o',
'o',
'd',
' ',
'a',
't',
' ',
'o',
'o',
'd',
' ',
'a',
't',
' ',
'w',
'o',
'd',
' ',
'a',
't',
' ',
'w',
'h',
'd',
' ',
'a',
't',
' ',
'w',
'h',
'a',
' ',
'a',
't',
' ',
'w',
'h',
'a',
't',
'a',
't',
' ',
'w',
'h',
'a',
't',
' ',
't',
' ',
'w',
'h',
'a',
't',
' ',
'J',
' ',
'w',
'h',
'a',
't',
' ',
'J',
'o',
'w',
'h',
'a',
't',
' ',
'J',
'o',
'n',
'h',
'a',
't',
' ',
'J',
'o',
'n',
'a',
'a',
't',
' ',
'J',
'o',
'n',
'a',
't',
't',
' ',
'J',
'o',
'n',
'a',
't',
'h',
' ',
'J',
'o',
'n',
'a',
't',
'h',
'a',
'J',
'o',
'n',
'a',
't',
'h',
'a',
'n',
'o',
'n',
'a',
't',
'h',
'a',
'n',
' ',
'n',
'a',
't',
'h',
'a',
'n',
' ',
' ',
'a',
't',
'h',
'a',
'n',
' ',
' ',
'd',
't',
'h',
'a',
'n',
' ',
' ',
'd',
'o',
'h',
'a',
'n',
' ',
' ',
'd',
'o',
'e',
'a',
'n',
' ',
' ',
'd',
'o',
'e',
's',
'n',
' ',
' ',
'd',
'o',
'e',
's',
',',
' ',
' ',
'd',
'o',
'e',
's',
',',
' ',
' ',
'd',
'o',
'e',
's',
',',
' ',
't',
'd',
'o',
'e',
's',
',',
' ',
't',
'h',
'o',
'e',
's',
',',
' ',
't',
'h',
'a',
'e',
's',
',',
' ',
't',
'h',
'a',
't',
's',
',',
' ',
't',
'h',
'a',
't',
'\'',
',',
' ',
't',
'h',
'a',
't',
'\'',
's',
' ',
't',
'h',
'a',
't',
'\'',
's',
' ',
't',
'h',
'a',
't',
'\'',
's',
' ',
'w',
'h',
'a',
't',
'\'',
's',
' ',
'w',
'h',
'a',
't',
'\'',
's',
' ',
'w',
'h',
'y',
't',
'\'',
's',
' ',
'w',
'h',
'y',
' ',
'\'',
's',
' ',
'w',
'h',
'y',
' ',
' ',
's',
' ',
'w',
'h',
'y',
' ',
' ',
'J',
' ',
'w',
'h',
'y',
' ',
' ',
'J',
'o',
'w',
'h',
'y',
' ',
' ',
'J',
'o',
'n',
'h',
'y',
' ',
' ',
'J',
'o',
'n',
'a',
'y',
' ',
' ',
'J',
'o',
'n',
'a',
't',
' ',
' ',
'J',
'o',
'n',
'a',
't',
'h',
' ',
'J',
'o',
'n',
'a',
't',
'h',
'a',
'J',
'o',
'n',
'a',
't',
'h',
'a',
'n',
'o',
'n',
'a',
't',
'h',
'a',
'n',
' ',
'n',
'a',
't',
'h',
'a',
'n',
' ',
'i',
'a',
't',
'h',
'a',
'n',
' ',
'i',
's',
't',
'h',
'a',
'n',
' ',
'i',
's',
' ',
'h',
'a',
'n',
' ',
'i',
's',
' ',
'r',
'a',
'n',
' ',
'i',
's',
' ',
'r',
'e',
'n',
' ',
'i',
's',
' ',
'r',
'e',
'a',
' ',
'i',
's',
' ',
'r',
'e',
'a',
'l',
'i',
's',
' ',
'r',
'e',
'a',
'l',
'l',
's',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
'r',
'e',
'a',
'l',
'l',
'y',
' ',
'g',
'e',
'a',
'l',
'l',
'y',
' ',
'g',
'o',
'a',
'l',
'l',
'y',
' ',
'g',
'o',
'o',
'l',
'l',
'y',
' ',
'g',
'o',
'o',
'd',
'l',
'y',
' ',
'g',
'o',
'o',
'd',
'.',
'y',
' ',
'g',
'o',
'o',
'd',
'.',
undefined,
' ',
'g',
'o',
'o',
'd',
'.',
undefined,
undefined,
'g',
'o',
'o',
'd',
'.',
undefined,
undefined,
undefined,
'o',
'o',
'd',
'.',
undefined,
undefined,
undefined,
undefined,
'o',
'd',
'.',
undefined,
undefined,
undefined,
undefined,
undefined,
'd',
'.',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
'.',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined ]

Thank you in advance.


/*jshint multistr:true */
var text = "Jonathan is really \ good at what Jonathan \ does, that's why \ Jonathan is really good.";
var myName = "Jonathan";
var hits = [];
for (var i = 0; i < text.length; i++) {
    if (text[i] === 'J'); 
    for (var j = i; j < (myName.length + i); j++) {
        hits.push(text[j]);
    }
}
if (hits.length === 0) {
    console.log("Your name wasn't found!");
}
else {
    console.log(hits);
}


#2

That printout is intense! Here are the mistakes I see:

line 5 - i isn't a variable, it's an index, so leave var out of for().
line 6 - you're missing the { after if() (add } at the proper spot too!)
line 7 - see line 5

Hopefully these changes will clear it up!


#3

@borgette, but it's correct way to declare for loop. From MDN:

Using for

The following for statement starts by declaring the variable i and initializing it to 0. It checks that i is less than nine, performs the two succeeding statements, and increments i by 1 after each pass through the loop.

for (var i = 0; i < 9; i++) {
console.log(i);
// more statements
}

I think lines 5 and 7 are both correct.


#4

Oops! You're right - var in lines 5&7 isn't a problem. The code passes with or without var.


#5

You're pushing the wrong value.

Instead of using .push(text[]) try entering a value that matches the length of your name.

Hope this helps partially.


#6

As @borgette said, problem with if statement. Should be:

if (text[i] === 'J') { // was missed {
    for (var j = i; j < (myName.length + i); j++) {
        hits.push(text[j]);
    }
} // was missed }

#7

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