Your second "for" loop


#1


So i wrote this script and somehow, somewhere i screwed up the loops probably, because it goes on and on forever, but i can not figure out where i might have been wrong. Could you please help me? I would be extremely happy to continue scripting c:

/*jshint multistr:true */
var text = "Csokifudzsolla vagyok, nem pedig Marci! \
Marci elment világgá, szóval később tala! Nem velem, \
hanem Marcival! c:";
var myName = "Marci";
var hits = [];
for (var i = 0; i < text.length; i++);
{
if (text[i] === "M");
{for (var j = i; j > myName.length; j++)
{hits.push("M", "A", "R", "C", "I")}
} 
}


#2

and

both have invalid syntax. Remove the semi-colon from the end of each.

The semi-colon ceases parsing at that point so the loop is missing a code body, as is the if statement.


#3

that's not the problem, i figured out the infinite loop problem, now it says "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."
what should i do with my code?

/*jshint multistr:true */
var text = "blablabla Marci bla bla Marci";
var myName = "Marci";
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)


#4

As expected, it is only the first problem needing to be addressed, so no, not THE problem, but definitely A problem.

Is the reason for the infinite loop.

Consider that j should be less than something while the loop is running, and once it is not, the loop will end. Also consider that the pointer i is moving through a long string of text, much longer than your name. When the first letter is matched in text, then j is set to start at the pointer and move to the right a number of characters. So i is the offset in each inner loop.

j = i; j < i + myName.length; j++

This should just be,

hits.push(text[j]);

#5

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