I really don’t understand what’s wrong with my code. I’ve seen people add +i after the myname.length to the second for loop but I dont get why.
Thank you.
/*jshint multistr:true */
var text = “Hi my name is Ilsse and I am from Mexico, I like to play video
games did i tell you my name was Ilsse right, I did”;
var myName = “Ilsse”;
var hits = ;
for (i=0; i < text.length; i++)
{
if (text[i] === “I”)
{
for (j=i; j > myName.length; j++)
{
hits.push([j]);
/*jshint multistr:true */
text=" what what what what what what what what what what what/ what Eric"
var myName= "Eric"
var hits = [];
for(var a=0 ; a<text.length ; a++)
if(text[a]=== myName[0]){
for(var b=a ; b<(a + myName.length) ; b++){
hits.push(text[b]);
}
}
var text = “Hi my name is Ilsse and I am from Mexico, I like to play video
games did i tell you my name was Ilsse right, I did”;
var myName = “Ilsse”;
var hits = ;
for (var i=0; i < text.length; i++)
{
if (text[i] === myName[0])
{
for (var j=i; j <(i+ myName.length); j++)
{
hits.push(text[j]);
}
}
I did what you did and I am still getting :
Oops, try again. There was a problem with your syntax.
@chiprunner59720 take a look a look at your code i have commented why you had the error and added some to it
var text = "Hi my name is Ilsse and I am from Mexico, I like to play video \
games did i tell you my name was Ilsse right, I did";
var myName = "Ilsse";
var hits = [];
for (var i=0; i < text.length; i++){
//here in your condition
// you can make it such that
//if your name changes you dont have to change anything
//by doing text[i] === myName[0]
if (text[i] === myName[0]){
//good idea to create a variable
// to store all the subsequent substrings
//from the second for loop
var found = "";
// here you had an infinite loop originally
//because you wrote j >myName.length
//and it will be true an always will be true
//when a match of my first initial is greater
//than the length of myName
for (var j=i; j <myName.length+i; j++){
//we are not going to push to hits yet
//hits.push(text[j]);
//as we iterate the text we are capturing
//all the characters and adding it to found
found+=text[j];
}//end second for-loop
//debugging statement
console.log(found);
// each time the second for loop is done the compiler
//comes down here and will check if the string captured in
//found so far matches myName entirely
//if it does then add it to hits
//else do nothing with it
if (found===myName) {
//now we push to hits
hits.push(found);
}
}//end if
}//you forgot to end the first for-loop here
console.log(hits);
the reason why he did a+myName.length is explained below
/* that loop begins when the current character
matches the first letter in myName. the loop will
start at the index where you got the match(i.e.a) and will
only run for the length of myName.
Now we write this statement j<myName.length+a adding the
'a' to ensure that we always begin the second for loop at the
index where we found the match
*/