5/7 toatally confused


#1

This is my code I have no Idea what to do from now and why.

/*jshint multistr:true */

var text= "Michael";"BMW";"BMW";"Michael";
var myName= "Michael";
var hits=[];

for(var i=0;i< text.length;i++);
if(text[i] === 'M');{
}


#2

Study this example (and the one at the start of the project (Lesson 1)) and see if you don't piece together what is expected:

/*jshint multistr:true */
var text, myName, hits;
text = "In diam diam, molestie Roy nec ullamcorper id, condimentum \
id mauris. Sed lacinia libero Roy vitae massa hendrerit vitae porttitor \
mauris porta. Praesent Roy dapibus vestibulum semper. Aenean posuere \
suscipit accumsan. Phasellus ac arcu Roy arcu. Phasellus sodales \
venenatis mollis. Fusce dignissim Roy mollis tortor sed fermentum. \
Pellentesque egestas, Roy mi in accumsan tempor, justo felis.";
myName = "Roy";
hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        for (j = i; j < i + myName.length; j++) {
            hits.push(text[j]);
        }
    }
}

#3

I get everything up to the if statement, which I partially understand. I'm confused about the zero part of how (text [i] === myName [0]) and everything beyond this point.


#4

myName[0] is the character at the first index of myName, or more simply, the first letter. In writing the code this way our program will work with any name, not just our own. Letter constants are never a good thing in re-usable code since they limit the universaliy.

Over to you...


#5

What about the second for statement and the push statement.


#6

In the above text, 'R' is found at around index 23. This is the value of i at the present time.

We set j to equal i, so our inner loop will begin iterating over text at index 23.

'Roy'.length is 3

... R o y
    2 2 2 2
    3 4 5 6
    |     |__
    |         i + myName.length = 26
    i = 23

The inner loop then,

    for (var j = i; j < i + myName.length; j++) {
        hits.push(text[j]);
    }

This will add one letter at a time to the hits array,

text[23] => 'R' => hits[0]
text[24] => 'o' => hits[1]
text[25] => 'y' => hits[2]

which will now look like,

[ 'R', 'o', 'y' ]

#7

Extra reading:

Array.prototype.push()

Also,

pop(), shift(), and unshift()

#8

I think I understand it except the j < i + myName.length


#9

The i is the offset, as we can see illustrated above. If we were just iterating over myName, the loop would look very much like the outer loop:

for (var j = 0; j < myName.length; j++) {
    console.log(myName[j]);
}
/*
R
o
y
*/

#10

/*jshint multistr:true */

var text= "Michael";"BMW";"BMW";"Michael";
var myName= "Michael";
var hits=[];

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


#11

Write the text as a single string value.


#12

/*jshint multistr:true */

var text="Michael BMW BMW Michael";
var myName= "Michael";
var hits=[];

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


#13

Remember to include the i offset:

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

and be sure to set j to i, not i to j


#14

/*jshint multistr:true */

var text="Michael BMW BMW Michael";
var myName= "Michael";
var hits=[];

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

Also, what do you mean by offset, like indented?


#15

The offset is the distance from the native starting index, 0. Above we started at index 23. That is the offset, and the current value of i in the example.


#16

No semi-colon in if statement syntax.

if (text[i] === 'M') {

#17

/*jshint multistr:true */

var text="Michael BMW BMW Michael";
var myName= "Michael";
var hits=[];

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

What is stopping it from working?, Why isn't i indented?


#18

j will be 22 or 23, depending if you count is correct or not. You probably shouldn't include this line in your code since the SCT might squawk.


#19

I meant to say why isn't I offset.


#20

/*jshint multistr:true */

var text="Michael BMW BMW Michael";
var myName= "Michael";
var hits=[];

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