5/7 This makes no sense


#1

var text = "c f diedhwdonc 3whfcnown Dan101iel ijcnoennd3 Daniel cec coenconr cDanielwnc nwnwcccncw"
var myName = "Daniel"
var hits = []
for (i = 0; i < text.length; i = i++){
if (text[i] === 'D'){
for (var j = i; j < (i + myName.length); j++){
hits.push(text[j])
};
};
}
every time i run this i get an infinite loop of death even though my loop looks the same as everyone else's. What am I missing


#2

should just be,

i++

The closing braces can all have the semi-colons removed, as well. They are not part of for or if statement syntax.


#3

i = i++

That's not right. "i++" is actually the exact same thing as "i = i + 1" so you're not allowing your i to iterate, instead it's confusing itself and won't work properly.


#4

Confound it @mtf. I work so hard!


#5

I know how you feel...


#6

then why did that work on the prev lessons


#7

Let's say i is currently at 0. The code body executes, then refers to this expression:

i = i++

What value will i become?

i++

which will be 1, so it will appear to work. But we overlook what i++ really is:

        i++  
    // is same as
        i = i + 1
    // or
        i += 1

so we don't need an assignment. The value is incremented on the fly.


#8

Im still not sure why i=i+1 works but not i = i++


#9

It's a double assignment that looks like this to the compiler:

i = i += 1

would be one explanation.


#10

ok, btw any reason you get the number of characters in your name x the amount of times your name is there, i got 18, instead of getting your name in the console or the number of time it found your name


#11

The program is storing letters, not the full name, so the count of the array is the number of letters. Divide that by myName.length for a found count.

Also, remember the program deliberately only matches the first letter so 'Daniel' and David ' will both be written to the array.

The name says, "Search text for your name" but in actual fact, it's really 'search text for the anything that starts with the first letter of your name.' We are encouraged to work at ways to improve this project, but on our own.