J < (myName.length + i) + hits.push(text[j])


#1



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


Hi, my code isn't behaving improperly - I copied the structure from the first segment of this lesson. But I am super confused about some of the elements that I don't feel are clearly explained (enough for dense ol' me, at least) in the prompts.

1.
I don't understand why the 2nd loop ends at (myName.length+i). What is the value of i here? Why shouldn't it just be j < myName.length?

2.
What the heck is this hits.push(text[j]) thing doing? Is someone able to help me break it down? I don't understand the similarity to .length.

Thank you for your time and help.

-Kayla


var text = "This is total bullshit, Kayla. \
you know you are better than this, Kayla."
var myName = "Kayla"
var hits = []

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


#2

@kaweisdorf,

1.I don't understand why the 2nd loop ends at (myName.length+i). What is the value of i here? Why shouldn't it just be j < myName.length?

Maybe if your read
https://discuss.codecademy.com/t/i-passed-this-but-i-just-need-someone-to-explain-the-second-for-loop/30895/2?u=leonhard.wettengmx.n

2.What the heck is this hits.push(text[j]) thing doing? Is someone able to help me break it down? I don't understand the similarity to .length.

The hits variable has an =empty= Array assigned to it.....
The push() Method will append the Value which you put within it's parentheses
to the array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function
http://stackoverflow.com/questions/6260475/how-to-the-operator


#3

Hi Leonard,

Thank you for that...everything in the link you attached for my first question really helps me make sense of what's going on except for when I get up to the exact place I posted I was confused about.

Let's say the length of my name is 5 (as it is). if rightindexfingerposition < (myName.length + leftindexfingerposition) and leftindexfingerposition started at the first value of myName (0), wouldn't my right index finger keep counting past the length of my name? Wouldn't something like rightindexfinger < 5/myName.length mean it stops counting at position 4, which will be the entire length of my name? What adding leftindexfingerposition to the end point actually saying?

Sorry if this is muddled, I'm just clearly very confused.

Thank you.


#4

@kaweisdorf

LIF = left index-finger
RiF = right index-finger
NL = name-length = 5
text = "you Know you are better than this, Kayla !"

       LIF                            LIF
        |                              |
    you Know you are better than this, Kayla !
        |----|                         012345
       RIF    ^                        |----|
              |                       RIF   ^
      j < LIF + NL                          |
                                    j < LIF + NL

Keep in mind that the FOR-loop internally always starts-counting with 0 ( zero )


#5

Ok, I think I kind of get it now, thank you. It's just still weird to me that if we know that in this instance

i=0=j="K" and myName.length=5 and we want j to stop at < position 5

that we have to say 5+0 for the computer to understand it, that it takes 5 as a set distance rather than a position value here whereas in other earlier lessons we could use myName.length as just a position value (or maybe I'm misremembering, I'm probably gonna do the whole functions and for loops exercises over again).

Best,

Kayla


#6

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