Totally stuck! probably a stupid error that i can't find


#1


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


'Oops, try again. 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.'

I tried some stuff, and looked on the forum, and even put in some console.log segments to try and see what part of the code was going wrong, but nothing seemed to help.


/*jshint multistr:true */

var myName = 'Johnny'
var text = 'The worst thing that could happen in the world is if Johnny got hit by a car. Johnny is also the most annoying person that i have ever seen! could you tell melissa that I do not want any Girlscout cookies. Johnny hates them';
var hits = [];

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


#2

the exercise url is the same for everyone, please copy paste your code to the forum so we can help you


#3

Sorry, i accidentally submitted the post while i was in the middle of creating it.


#4

conditions of both your loop evaluate to false immediately, so neither loops run


#5

The code of the first for loop, and first if loop were created in previous lessons, so i am not sure why it is not running. Also, could you specify why it is evaluating to false?


#6

for the first iteration of the loop, we would have the following condition:

0 > 223

which is false, the loop will run for as long as the condition is true


#7

I was under the impression that that was the end point, and the for loop would run until that was true.
for (start-point; endpoint; increment) {
body;
}


#8

yes, the loop runs as the end point condition is true

your end point condition is false the first time its evaluated


#9

I believe you misunderstood what i said, sorry for the confusion. I thought that the code would run while the second segment was false, and end when the second segment became true. Thank you for clearing that up.


#10

no, its the other way around. The loop runs while condition evaluate to true

now you figured this out, you should be able to solve the problem?


#11

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