I want to understand 'for' loops better


#1

I am stuck at this part. I would first off like to understand what \ stands for and how that works out as well as .push().

my current script reads

var text = "Hey, how are you \ doing? My name is Eric. Eric loves to party hard\ Eric is America \ Who's that \ oh its just Eric";
var myName = "Eric";
var hits = [];

for (i= 0;i > text.length;i++); {
    if (text[i] === 'E') {
        for ( j = i; j > (i+myName.length); j++); {
            hits.push(Text[j]);
        }
    }
}

I get the error stating

" 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. "

@leonhard_wettengmx_n , if you could explain \ and .push () , that would be great.


#4

@solamen,
the \ character is interpreted by the Javascript-interpreter
as a so-called < escape > character

  • causes for the next-character not being interpreted,
    and is normally used to suppress a line-feed character at end of a line
    and its used for instance in a Multi-line-string defintion for readability like
var text = "Hey, how are you \
 doing? My name is Eric. Eric loves to party hard\
 Eric is America \
 Who's that \
 oh its just Eric";
  • is not taken into account in the text-array.length-property

====================================

var text = 
"12345\
67890"
console.log( text.length ); //=>output=>10
for (var i=0; i<text.length; i++) {
    console.log(i,text[i]);
}

/*=>output
0 '1'
1 '2'
2 '3'
3 '4'
4 '5'
5 '6'
6 '7'
7 '8'
8 '9'
9 '0'
*/
//but disaster is looming when used as
var text =
"12345\67890"
console.log( text.length ); //=>output=>9
for (var i=0; i<text.length; i++) {
    console.log(i,text[i]);
}
/*>>output
0 '1'
1 '2'
2 '3'
3 '4'
4 '5'
5 '7'
6 '8'
7 '9'
8 '0'
*/

the 6 is missing.


#5

@solamen,
The =data-types= in Javascript are

•Number
•String
•Boolean
•Symbol
•Object  ◦Function
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

Now as you are defining the variable hits
you are assigning an EMPTY-Array to this _hits variable
var hits=[]; // is [ and a ]
In Javascript-talk they say...
the hits-Array is of the prototype-Array
and if do a search in ==the Book== using
javascript array prototype site:developer.mozilla.org
you'll find
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Now if you look at the Web-page,
and then look in the left-hand column,
under the header Standard built-in objects
you see properties like the length-property
and methods
(these are predefined function's, which you could use =on= the =hits= variable using the Method-specific syntax. (RT.M) )