Your second "for" loop


#1


someone help me.

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


var text="lgsdlsd,lkjsdbhdkjs,lkhsdf,klmkas";
var myName = "Nishat";
var hits=[];
for(var i=0;i< text.length;i++) {
    if(text[i] === "N"){
        for(var j=i; j <(i + myName.length);j++)
        {
            hits.push(text[j]);
        }
    }
}
console.log(hits)


Second for loop isnt pushing anything into an array
#2

Seemingly, your second for loop should look something like this:
for(var j = i; j < (myName.length + i); j++);
hits.push("j");
Also, all of your { are unnecessary to completing this lesson so you can get rid of them
All in all I ran your code back through with the fix and it passed for me so hopefully that helps!


Your second "for" loop
#3

again error :cry:

var text="lgsdlsd,lkjsdbhdkjs,lkhsdf,klmkas";
var myName = "Nishat";
var hits=[];
for(var i=0;i< text.length;i++) {
    if(text[i] === "N"){
        for(var j=i; j <(myName.length+i);j++)
        {
            hits.push("j");
        }
    }
}
console.log(hits)

#5

I'm not sure why you are suggesting to push the character "j" each time the loop runs. What's your plan there?


#6

When I run your code, this is the error message it gives me:

It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

That message is telling you exactly what is wrong.

Do you see the problem now?


#7

Obviously I just said above it passed in the codecademy lesson so the plan there was to help sabbasachi pass


#8

I think a better plan might be to help @sabbasachi learn to code. These two things are not necessarily the same. :slight_smile:


#9

This is my full code that I got when I fixed your earlier code above. I just ran it again and it passes the lesson. Seemingly, the only difference is that you don't have a ; after your ;j++) and I just ran the code without the ; after that and it gave me the same error that you were getting. This would seem to be the problem then.
var myName = "Nishat";
var hits=[];
for(var i=0;i< text.length;i++)
if(text[i] === "N")
for(var j = i; j < (myName.length + i); j++);
hits.push("j");
console.log(hits)


#10

Here, you are still filling the hits array with the character "j". When this is done correctly, the hits array should be filled with 'N', 'i', 's', 'h', 'a', 't'.


#11

"Nishat" is not in text... Not even "N"...


#12

Could someone please explain what's going on in the hits.push() part of the code?

I understand that the second for loop is telling the console to print the given amount of letters in the matrix (including and after the trigger letter) according to the length of your name.

However I don't understand how the hits.push() is working. More specifically, what is going on inside the parenthesis of the push(). When you type text[j] inside the hit.push(), what are you telling the code? How would typing something other than text[j] inside hit.push() change the outcome of the code?


#13

@alphawarrior hits.push(text[j]) means put text[j] into hits


#14

@bibleman13 Yes this might pass:

for(var j = i; j<t; (myName.length + i); j++); 
       hits.push("j");

but not because it does what it is expected to do but because you found a way to trick the test function. Meaning that:

  • you have a 2nd loop (check)
  • you push something to the hits array (check)

but neither is this push inside of the second loop nor is the pushed value remotely close to your name.

As you can read here:


(this is for conditions but works for loops as well) The semicolon after the for loop is the statement that is executed when looping. So what you're doing is executing this statement

/*do absolutely nothing*/;

name.length times and after even the first for loop is done you add "j" to your hits array. So you fulfill some kind of minimum requirements to pass but obviously your program is not doing what it is supposed to do.

Take home message: Be careful with omitting {} unless you're aware of what you're doing and double check if your program really does what it is supposed to do because when you code stuff for yourself or even as a job there is no test function that tells you that something is wrong.

@alphawarrior .push() used on an array just appends the value in it's parenthesis to the end of the array. For example:

a = []
a.push(1)
console.log(a); // -> [1]
// or multiple values at once:
a.push(2,3)
console.log(a) // -> [1,2,3]
// and the value of the statement itself...
console.log(a.push(4,5)) // -> 5
// ... is the new length of the array

or if you google command + javaScript you might find this explaining it as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push


#15

Well very interesting. Obviously I was not trying to trick anything lol I had always wondered that when it passed and it did not give back my name or whatever but usually I could put console.log(hits) at the end or something and it would fill the requirement the lesson was looking for. Good posts as well on the forum I will have to reference those. Hopefully not to much trouble has been caused obviously I do not want people to copy my mistake I had just never known that part of the coding was wrong I just figure it was style or something because it pass. Thanks for the info and thanks to @albionsrefuge as well for point out the error continuously even when I did not get it at first. :smiley:


#16

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