5/7 I need Help, frankly


#1

Ok so my code looks like
/*jshint multistr:true */
var text = "Angela Angela Angela";
var myName = "Angela";
var hits = [];
for(var i = 0; i < text.length; i++){
if (text[i] === "A") {
for(var A = i; A === i + myName.length; i++) {

};

}
};

And it's completely wrong. To be honest, I have no idea what I'm doing. I'm just guessing at this point. .push sounds like a foreign language. Basically, I messed up and I need help. Does anyone know how to fix it? Thanks!!

Seriously, thanks.


#2

try this

var text = "Angela Angela Angela";
var myName = "Angela";
var hits = []; 
for(var i = 0; i < text.length; i++){
    //lets make it more dynamic and
    //compare it like this so then when the
    //value of myName changes we dont have to change
    //anything
    if (text[i] === myName[0]) { 
        //create a variable to store subsequent substrings
        found = "";
        
        /*this loop begins when the current character
        matches the first letter in myName. the loop will
        start at the index where i got the match(i.e.i) and will
        only run for the length of myName.
        
        Now we write this statement A<myName.length+i adding the
        'i' to ensure that we always begin the second for loop at the 
        index where we found the match
        
        */
        for(var A = i; A <i + myName.length; A++) {
            //as we iterate the text we are capturing
            //all the characters and adding it to found
            found += text[A];
    }
        // each time the second for loop is done the compiler
        //comes down here and will check if the string captured in 
        //found so far matches myName entirely
        //if it does then add it to hits
        //else do nothing with it
        if (found===myName){
            hits.push(found);
        }
    }
}

console.log(hits);

#3

Not really. :smiley:

It's actually very close to what you have to do.

First, your for loop is really close:


In the hint, it says this:

Your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable.

So, it's myName.length = i, right? Exactly what you did. :smiley: Except it says to stop when it hits the value of i. So, what you need to do is use < instead of ===. It was close though. :smiley:

And:

It's actually something really simple.:smiley: Think of it as something that pushes words/numbers/booleans inside an array.

So like:

var b = [];
b.push("ME BUNNY")

Result:

var b = ["ME BUNNY"];

Get it? Hope you do. :smiley: You just do the same thing, by pushing your 'A' to hits!


#4

@rydan Thanks for the help!!! It was really thoughtful of you to type up the entire code and add in comments. Thank you!


#5

@ragezapper Thanks for the encouraging comments! The fact that you explained all of it step by step was awesome, by the way. Thanks again!!