Code works but didn't understand why, please help!


#1

Hi all,

I've written the code below, following Codeacademy's step by step guidelines, but I have NO idea what the code does/is supposed to do. Would really appreciate if someone can explain the code to me:

/*jshint multistr:true */

var text="shaurya gaf shaurya gaf shaurya gaf shaurya gaf shaurya gaf shaurya gaf"
var myName="shaurya"
var hits=[]

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

if (hits.length === 0) {console.log("Your name wasn't found!")} else {console.log(hits)}


#2

Just FYI - the code works but I have no idea what it's supposed to do..


#3

Did you author this code or you got it from somewhere?


#4

@rydan I authored this code under the direction of Codeacademy's Javacript course


#5

@codecoder81953

First

We create a variable called text containing a string with the text

Then

We create a variable myName that has like value a string with the name shaurya (which is your name).

Next

We create an empty array called hits.

After

We create a for loop. Inside it, we create the i variable that is our counter which has like value 0 in it. As long as variable i is smaller than the length of text variable, we will increment variable i. If the variable text indexed with variable i (our counter) is equal to s which is the first letter of your name, we create another for loop. In the loop, we create variable j which has the value of variable i. As long as variable j is smaller than the length of variable myName plus the variable i, we increment variable j. We use push method to add the character indexed variable j in text variable to the hits array.

Finally

If the length of hits array is equal to 0, we log to the user:

If not, then we log the user the hits array.

Hope this clarifies your understanding!:wink:


#6

Take a look look at this POST it has a similar code with explainations


#7

This was very detailed and VERY helpful, thank you so much! Just a few questions:

1)

Does this mean every time "s" comes in the text variable, it will move on to the next for (even if it is an "s" that is not part of my name)?

2)

How do I control what character gets pushed every time something is indexed by j in the text variable?


#8

Thanks for the link @rydan, that was a little helpful - i'm still confused though because in the other thread you were looking at it from a debugging POV, but i'm asking for an explanation. Also, if I change my code to incorporate a sentence instead of a random list of words, the result gets really screwed up:

/*jshint multistr:true */

var text="Hi my name is shaurya and I like to play games. shaurya is my name and I really hope you call me shaurya because I like shaurya.Thanks!"
var myName="shaurya"
var hits=[]

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

if (hits.length === 0) {console.log("Your name wasn't found!")} else {console.log(hits)}

Thoughts?


#9

@codecoder81953
1) Well, yes...
2) In fact, the exercise doesn't ask you to control what gets pushed every time something is indexed by j variable in the text variable. The for loop for the j variable which is the counter will add the character indexed by j variable in text variable.
Let's say, variablei is equal to 0. The if statement realizes he found a "s". Then, since j variable is equal to i variable (which means he's now equal to 0), as long as j variable is not equal to the length of myName variable (7 characters) + i (7 becausei is equal to 0), it will add the character to the hits array (an "s"). After, it incrementsj and goes to index 1 of text variable. Is it still lower than the length of variablemyName? Yes, in this case variable j is equal to 1 which is lower than 7. We add this character to our hits array and increment variable j. It keeps looping like this until variablej is not lower than myName length,


#10

take note that the last but one character in your text is "s"(same as myName[0]) which means when the loops gets to that character

will be true and the second for loop begins.
Note on the second for loop:

/*      that loop begins when the current character
        matches the first letter in myName. the loop will
        start at the index where you got the match(i.e.i) and will
        only run for the length of myName.
        
        Now we write this statement j<myName.length+i adding the
        'i' to ensure that we always begin the second for loop at the 
        index where we found the match
        
        */

The second for loop starts pushing characters to hit starting at the last 's' so the next character will be the
!
Then after there are no more characters but your code says keep adding so**undefined** will be pushed to yor hits array. Thats why you are getting those weird elements in your array


#11

Aaaaah

This makes sense now!

Final 2 questions (I promise, I'm nearly there!)

1) What if I wanted the "for" to run for my entire name instead of just "s"?
2) What if every time "s" came up in text, it shows a number instead of the letter? (basically customising what we are shown)