Not pushing value to the hits array


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661



var text = 
"Look here doug, I've got a big douger. And you, you don't have \
a big douger. And for that I am very sorry. I hope you understand, \
that the size of the doug isn't the best thing to have. But it sure is handy. \
doug, doug, doug, doug";

myName = "doug";
var hits = [];

for (i = 0; i < text.length; i++)
{if (text[i] === "d"){
    for(var j = i; j <= (myName.length+i); j++);
}
newArray = [];
newArray.push('doug');
newArray[0];
}


#2

var text = 
"Hey Doug, See you are looking fine. Doug";

myName = "Doug";
var hits = [];

for (i = 0; i < text.length; i++)
{if (text[i] === "D"){
    for(var j = i; j <= (i+myName.length-1); j++)
    hits.push(text[j]);}

//newHits = [];
//newHits.push('doug');
//newHits[0];}
}
if (hits.length === 0)
    {console.log("Your name wasn't found!");}
    else {
        console.log(hits);}

I was able to change around my code a bit
added hits.push instead of newArray.push.
As well as adding (j <= (i+myName.length-1) to get the correct "Doug".
It ended up working and i'm somewhat happy with how I got there, But still a little confused. If anyone can add anything to help me understand better. That would be great.

Thanks in advance.


#3

confused about what? Please explain what you do and don't understand so i can fill in the blanks

why <= (i+myName.length-1), then simply use smaller then (<) so you don't have to subtract one, its cleaner


#4

(var j = i; j < (i + myName.length); i++) - Thank you for pointing that out.

As for the whole code I'm just gonna type how I understand it, and you correct me if I am wrong.

var text - the string in which my "for" loop is going to operate in.
var hits - my array ( empty array )
first part of "for" loop
for(i = 0; i < text.length; i++) - This loops my string text in increments of +1
{if (text[i] === "D") - signifies when the loop finds "D" execute the following loop.
for (var j = i; j <(i+myName.length);j++) - This loop will have start from i (which is defined as
"D" now and run until the end of the length of var myName. in increments of +1.
hits.push(text[j]); - this pushing return info from for loop into empty hits[]

Anything I missed, or communicated wrong on this?


#5

sounds you understand it perfectly :slight_smile:


#6

fantastic, now I need to rinse and repeat.
Would you say repetition is critical?


#7

I wouldn't see repeat is critical, practicing on the other hand is :slight_smile:


#8

:slight_smile: understood
Thank you for responding, I am sure we will talk again sometime.
Back to more work!


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.