5/7 (2nd "for" loop)


#1

What am i doing wrong here. I have no idea whatsoever. It's probably something obvious, but I've been stuck here for three weeks. May god have mercy on my soul.

/*jshint multistr:true */
var text = "My name is William. Today we will be learning about\
the history of my anscestors. William. Nevermind... William is too cool for that;
var myName = "William";
var hits = [];
for (var i = 0; i < text.length; i++);{
if (text[i] === "W");{
for (var j = i; j < (myName.length + i); j++) {
hits.push(myName[j]);
}
}

}
}

Some help would be much appreciated. Thanks for dealing with my ignorance.


#2

I believe your issue is related to these two lines of code:

for (var i = 0; i > text.length; i++){
                  ^

for (var j = i; j > (myName.length + i); j++) {
                  ^

The condition has to prove true for the code to work in the for loop. Right now, there's no way that i is greater than the length of the text variable,

On a similar point, j is also not greater than the variable myName, and i put together.


#3

Thanks! It still does not work though. It gives the error message make sure you push the letters to the hits (something of the sort). Could you possibly explain the .push command to me? I'm very confused.


#4

Could you update your code? Also try to censor out, or remove any words that aren't strictly PG, this is a forums for members of all ages.


#5

Done for him, under language guidelines.


#7

Apologies for the vulgarity. In the future I will make any and all attempts to keep a level head in the heat of frustration. Here is the updated code:
/*jshint multistr:true */
var text = "My name is William. Today we will be learning about\
the history of my anscestors. William. Nevermind.... William is too cool for that";
var myName = "William";
var hits = [];
for (var i = 0; i < text.length; i++);{
if (text[i] === "W");{
for (var j = i; j < (myName.length + i); j++) {
hits.push(myName[j]);
}
}

}

Again, probably an easy fix. I hope the language used here is to your satisfaction (no sarcasm it's just how i sound :smile: )