Search Text for Your Name


#1

Hi all, hope somebody can help, I found several threads talking about it already but nobody really nailed down the problem nor explained it. All guides I found online are seriously unclear.
So I got stuck in UNIT 3: 'FOR' LOOPS IN JAVASCRIPT at parts 4 and 5.
I loved all of it so far, but the guide stressed way too much on how to start and end FOR loops, but didn't explain push AT ALL, so I am rather frustrated and trying to figure out how things work, failing at it.
I couldn't get past the exercise 4 (https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/4) and I also tried to skip it, thinking maybe the next one will enlighten me, but nope.

var text = "Flavio sta codificando Javascript e non lo ha mai fatto prima. Flavio è \
figo";
var myName = "Flavio";
var hits = [];/*jshint multistr:true */
for (i=0; i<=text.length; i++) {
    if (hits[i]===myName[0]) {
        for (j=1; j<=(myName.length + 1); j++) {
            if (hits[j]===myName[1]) {
            hits.push(myName[j]);
            }
        }
    }
}

This is the code I have, can I please have some good soul explaining me what exercises 4 and 5 really want me to do and, more importantly, why and how? Because apparently the code above ("second 'for' loop isn't pushing values to the hits array. "), and I am totally clueless, the "Hint" is completely useless and worthless.

Thanks in advance!


#2

This line should be above everything on the top line of the code.

We want to check text[i] since that is what we are iterating over.

Should read,

var j = i; j < i + myName.length; j++

Should read,

    hits.push(text[j]);

#3

Dang I hate this forum layout, it's so minimalistic that I struggle to find things (which is why I am not multi-quoting like I usually do), and do note I am either mod/admin in nearly all the forums I am active on :smiley:
Rant apart, it works great, thanks a lot in the first place! What I cannot understand however, is how the whole thing really works, because now I have this code (although the parenthesis one works regardless, but I removed it anyway, I suppose it's better coding since you are a mod you should know better than a guy at his 2nd day, ha)

/*jshint multistr:true */
var text = "Flavio sta codificando Javascript e non lo ha mai fatto prima. Flavio è \
figo";
var myName = "Flavio";
var hits = [];
for (i=0; i<=text.length; i++) {
    if (text[i]===myName[0]) {
        for (j=1; j<myName.length + 1; j++) {
           hits.push(text[j]);
        }
    }
}

I just don't get how the push thing works, and why I have to use hits.push(text[j]) instead of whatever else.


#4

I didn't notice this before, it should read,

i < text.length

else it tries to read past the end of the string.

This project has us append the hits array with one letter at a time, regardless what the letter may be. The only match is the first letter. After that its just a count. Each time a match is found, myName.length letters are pushed on to the array, one at a time.

hits.push(text[j]);

.push() is an array mutation method. It's opposite is .pop(). When we push to the array, we append it. When we pop from the array, the last element is returned and removed from the array. Last on, first off, as the saying goes.

Read up on push(), pop(), unshift() and shift() for a better understanding of these array methods.


#5

Thanks, that's exactly what I needed to understand! Have a wonderful day! :slightly_smiling: