Search name for text, nothing in my hits array


#1

/*jshint multistr:true */
var text = "Hello my name is Sel. I'm always asked where my/ parents got the name Sel, then i tell them its a family name. Every 5th male gets Sel as sur a name";
var myName = "Sel";
var hits = [];
for(var i=0; i if (text[i]==="S"){for(var j=i+myName.length; j<=i+myName.length; j++){
hits.push(text[i]); }; }; };

if (hits.length=0){
console.log("Your name wasn't found!"); }
else { console.log(hits); }

when i click submit it says that it is correct. but how come nothing was pushed into the hits array ?


#2

for(var i=0; i if (text[i]==="S"){for(var j=i+myName.length; j<=i+myName.length; j++){
 hits.push(text[i]); }; }; };

This jumbled code here is a little hard to understand. I suggest never coding like this without absolute necessity. I've decoded it a little and I believe I understand the problem.

for(var i=0; i if (text[i]==="S"){
  for(var j=i+myName.length; j<=i+myName.length; j++){
    hits.push(text[i]); 
  }; 
}; //No need for semi-colon after closing brackets.
}; //Extra closing bracket

syntax for a for loops looks similar to this:

for (var i = 0 ; i < 10 ; i++){
    //Code and stuff
}

So the code you're trying to do in those lines should look more like this:

for ( var i = 0 ; i < text.length ; i++)
{
	if (text[i] ===  "S") 
	{
		for(var j = i ; j < (myName.length + i) ; j++)
		{
			hits.push(text[j]);
		}
	}
}

I see where you were going, honestly I do. Unfortunately though it was slightly off the beaten path, and it's just better to make things a little more readable for both yourself and your audience. I hope this helps!


#3

i had the first for loop like that idk why it came out different than what i had, and sorry for the mess lol. As for the second loop the extra i had on there was the ;'s and that i had j= i +myName.length. Even though i have done the changes you gave me i still have nothing in my hits array when i click submit. its telling me its correct but i don't want to move on to the next topic if it is supposed to have letters in my hits array.


#4

Alright that's fair enough. Why don't you post your new code and we'll see what changes need to be made if any. It could just be a browser issue as well..


#5

/*jshint multistr:true */
var text = "Hello my name is Sel. I'm always asked where my/ parents got the name Sel, then i tell them its a family name. Every 5th male gets Sel as sur a name";
var myName = "Sel";
var hits = [];
for(var i=0; i < text.length; i++) {

     if (text[i]==="S") {

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

             hits.push(text[j]); 

             } } }

if (hits.length=0) {

    console.log("Your name wasn't found!"); }

else { console.log(hits); }

here you go, tried to space out so it doesn't look messy.


#6

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

add a parenthesis around i+myName.length

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

#7

i got figured out. i really appreciate the help :smile:

here's my working code:

var text = "Hello my name is Sel. I'm always asked where my parents\ got the name Sel, then i tell them its a family name. Every 5th\ male gets Sel as sur a name";
var myName = "Sel";
var hits = [];
for(var i = 0; i < text.length; i++) {

     if (text[i]==="S") {

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

             hits.push(text[j]); 

             } } }

if (hits.length === 0){

    console.log("Your name wasn't found!"); }

else { console.log(hits); }

what made it work was changing the last if statement from : if(hits.length=0) to if(hits.length===0)


#8

Ah! I didn't even see that! Fantastic, have a good one!