Second for loop


#1

Hi good coders.
the second for loop isn't functioning. I read all the answers and tried them, nothing seem to be helping. any Ideas?
thanks in advance

var text ="my name is mahmoud shahin";
var myName="Shahin";
var hits = [];
for(var i=0;i< text.length ; i=i+1){
if(text[i] === myName[0] ){
for(var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
``;
any Idea what's happening here


#2

@mahmoudshahin, before second loop you check does var text contain the 1st symbol of var myName. It means that you try to find in var text symbol 'S'. So, condition if(text[i] === myName[0] ) always returns false, because var text doesn't contain symbol 'S'. Try:

var text ="my name is Mahmoud Shahin";

#3

This is working code! var text have to start with the first letter of your name. Check it out in example bellow.

var text = "Nemanja se ubi sa ovim kodom.";
var myName = "Nemanja";
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]);
}
}
};


#4

Name can be anywhere in the var text. Why do you think so???
His problem was that he trying to find symbol 'S' which is not in var text.


#5

Yes, the name can be anywhere in the var text


#6

hahahah what a clumsy mistake :smiley: thanks mate


#7

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