5/7 Explain My Output


#1

I thought I understood the code, but I don't understand my output.

var text = "this is a Joanne string of random Joanne text";
var myName = "Joanne";
var hits = [];

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

When I submit that code, it prints "12". I think I don't understand what hits.push(text[j]) does. I know that i = J at position 10, and i (10) + myName.length (6) results in position 16. I would expect that if I'm adding 1 + j it would give me position 11 to get the next letter of my name, so I'm not sure where 12 comes from. Any explanations for how the result is calculated and what that push code does would be appreciated - thanks!


#2

hi Joanne,
It probably returns 12 because hits.length = 12 at the end of your program. Hits will contain the letters of your name twice (12).
hits.push(text[j]) adds the character at position j in text at the end of the hits array

I guess you were confused about characters and indexes being pushed here. Nowhere are you adding 10 (position of "J" to myName.length (which would result in 16) ) you start at 10 and iterate through text until j = myName.length. So when j hits 16 (i + myName.length) the for loop is not executed anymore.

So 12 refers to the length of hits and not to any position in your text variable.


#3

Hii once again I don't understand this code as well as the outcome....

Here is my code:

/*jshint multistr:true */
var text = "Pradeep Blah Blah Blah Blah Blah Blah Pradeep Blah / Blah Blah Blah Pradeep Blah Blah Blah";
var myName = "Pradeep";
var hits = [];

for (i = 0; i

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

};

First of all I cleared this code but i don't understand this code... I want to ask:

Here whats the difference in "<" and "<=" (i.e. both the for loops) and in this what is the meaning to write "i+" in j loop? and want to understand purpose of .push method? and why it is important here?

And last but not least the outcome... here shows only total of my name i.e. 21 - Pradeep = 7 * 3(Since it is written 3 time in text).. please help me to understand. Thanks in advance.

Regards,
Pradeep.


#4

Hi Pradeep,

The difference between < and <= is that with a < 6 the loop runs until 5. When a = 6 it stops. With a <= 6 it will also run when a = 6. You run the for loop while j < i+myName.length. At the first time text[i] = "P" i = 0; i + myName.length = 7. text[7] = " " (a space) since we started counting at 0 instead of 1; space is not part of your name so should not be added at the end of hits.
That's what the hits.push[text[i]) does: it just adds the text[i] at the end of the hits array.

The outcome of 21 is exactly what you are saying: Pradeep is 3 times in hits.


#5

So here I should write <= not just < because i want to run loop to myName.length times i.e. Pradeep?


#6

Oh ok I got it thank you so much..


#7

Try it out, I'd say.
Just remember what I said about the positions in text and myName. Remember myName.length = 7 and myName's last position/index = 6

If you iterate over myName while i <= myName.length what will be the last position your program will try to reference? It will try to reference myName[7] which does not exist. Same with text[text.length]. It will reference a non existing position.

Got the answer now?


#8

I saw Predeep's reply too late.
Good that you got it now!


#9

I also don't understand my output. Codecademy says this 'passes' and gives me the green light, but it's not actually functioning the way it's supposed to. Here is my code:

/*jshint multistr:true */

var text = "Hey, Milo. How are you, Milo? I'm so glad you are feeling better, Milo.";
var myName = "Milo";
var hits = [];

for (var i = 0; i < text.length; i++) {
if (text[i] === "M"); {

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

}

console.log(hits);

Here is my output:

[ 'H',
'e',
'y',
',',
'e',
'y',
',',
' ',
'y',
',',
' ',
'M',
',',
' ',
'M',
'i',
' ',
'M',
'i',
'l',
'M',
'i',
'l',
'o',
'i',
'l',
'o',
'.',
'l',
'o',
'.',
' ',
'o',
'.',
' ',
'H',
'.',
' ',
'H',
'o',
' ',
'H',
'o',
'w',
'H',
'o',
'w',
' ',
'o',
'w',
' ',
'a',
'w',
' ',
'a',
'r',
' ',
'a',
'r',
'e',
'a',
'r',
'e',
' ',
'r',
'e',
' ',
'y',
'e',
' ',
'y',
'o',
' ',
'y',
'o',
'u',
'y',
'o',
'u',
',',
'o',
'u',
',',
' ',
'u',
',',
' ',
'M',
',',
' ',
'M',
'i',
' ',
'M',
'i',
'l',
'M',
'i',
'l',
'o',
'i',
'l',
'o',
'?',
'l',
'o',
'?',
' ',
'o',
'?',
' ',
'I',
'?',
' ',
'I',
'\'',
' ',
'I',
'\'',
'm',
'I',
'\'',
'm',
' ',
'\'',
'm',
' ',
's',
'm',
' ',
's',
'o',
' ',
's',
'o',
' ',
's',
'o',
' ',
'g',
'o',
' ',
'g',
'l',
' ',
'g',
'l',
'a',
'g',
'l',
'a',
'd',
'l',
'a',
'd',
' ',
'a',
'd',
' ',
'y',
'd',
' ',
'y',
'o',
' ',
'y',
'o',
'u',
'y',
'o',
'u',
' ',
'o',
'u',
' ',
'a',
'u',
' ',
'a',
'r',
' ',
'a',
'r',
'e',
'a',
'r',
'e',
' ',
'r',
'e',
' ',
'f',
'e',
' ',
'f',
'e',
' ',
'f',
'e',
'e',
'f',
'e',
'e',
'l',
'e',
'e',
'l',
'i',
'e',
'l',
'i',
'n',
'l',
'i',
'n',
'g',
'i',
'n',
'g',
' ',
'n',
'g',
' ',
'b',
'g',
' ',
'b',
'e',
' ',
'b',
'e',
't',
'b',
'e',
't',
't',
'e',
't',
't',
'e',
't',
't',
'e',
'r',
't',
'e',
'r',
',',
'e',
'r',
',',
' ',
'r',
',',
' ',
'M',
',',
' ',
'M',
'i',
' ',
'M',
'i',
'l',
'M',
'i',
'l',
'o',
'i',
'l',
'o',
'.',
'l',
'o',
'.',
undefined,
'o',
'.',
undefined,
undefined,
'.',
undefined,
undefined,
undefined ]


#10

Take the semi-colon out

if (text[i] === "M"); // See the semi-colon, now take it out :smile:


#11

I do not understand the output either. Codeacademy says: way to go and go to the next lesson, but the code fails. The output is "12"

var text="Hi Kim, hoe gaat het met je? \
We zoeken in deze tekst naar het woord Kim. \
Waarom we naar Kim zoeken? \
Geen idee, om Kim te vinden denk ik.";

var myName="Kim";

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]);
}
}
}


#12

Oops I forgot console.log(hits);