Correct now, but why?


Knew I was probably overlooking something minor, but now moved the 'for' loop parentheses next to 'for' instead of 1 space away, (as was done in previous lessons with no problem), but here, this change caused it to go from Oops, looks like 2nd loop isn't pushing anything to array, make sure it's working properly, and make sure name appears in text string (paraphrasing very slightly). - to now being marked Correct - but without sending anything to the console, even though console.log is included. Code = (marked wrong before now, only difference then was a single space after the words 'for'): (so the question is why did this change cause it to now be correct, and why did nothing go to the console?) Any insights appreciated, Thanks very much...

Additional Note: still says this about it: "JSLint was unable to finish. - Unexpected 'for' - for(var i = 0; i < text.length; i++) { - Unexpected 'var'. - for(var i = 0; i < text.length; i++) { - Function Report = There are no functions."

var text = "Robert some Robert text Robert";
var myName = "Robert";
var hits;
for(var i = 0; i < text.length; i++) {
if (text[i] === "Robert") {
for(var j = i; j < (i + myName.length); j++) {


We can write,

for (var j = i; j < i + myName.length; j++) {


The white space is not a problem, and actually recommended for readability.

In the above line, hits is declared, but not defined. Since it is not defined as an array, we cannot use Array.push(). That would be a type error or we would be told that hits does not have a push attribute.

var hits = [];

This should be just the first letter, R or it could be the dynamic, myName[0] so any name can be used.

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

This should be outside of all the loops, at the bottom of your code.


Thanks a lot, made the changes & it sent this (which I imagine it was supposed to in this format) to the console: (& sorry for the missing 'hits' brackets, they were there for days but in my many attempts, must have erased them somehow).

[ 'R', 'o', 'b', 'e', 'r', 't', 'R', 'o', 'b', 'e', 'r', 't', 'R', 'o', 'b', 'e', 'r', 't' ]


The code is posted everywhere so you can remove it. Just leave the output you've shown. This is the correct output, as expected.