5/7 your second "for" loop / Help?


#1

This is my code. If you know what I did wrong, please help and explain because I don't understand:


/*jshint multistr:true */
var text = "Shae Shae Shae" ;
var myName = "Shae";
var hits = [];

for (var i = 0; i if (text[i] === 'S') {
for(var j=i; j < (i+myName.length); j++); {
hits.push( text[ j ] );
}
}
}
console.log(hits);


It works but this is what it returns:
[ ' ', ' ', undefined ]

If you can help, thanks!


#2

@shaekelly,

The for statement creates a loop that consists
of three optional expressions,
enclosed in parentheses and separated by semicolons,
followed by a statement or a set of statements executed in the loop.

Syntax

for ([initialization]; [condition]; [final-expression]) {
    statement
}

As a semi-colon-; is an End-of-Statement indicator for the Javascript interpreter,
and you are using the semi-colon in your FOR-loop definition,
like

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

the statement-body will NOT be executed..........

Remove the semi-colon-;


#3

@leonhard_wettengmx_n

I removed the semi-colon, but it resulted in the same output. Any other ideas?


#4

What is this FOR-loop statement of yours ...???

Please read
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for