I need help on my second loop


#1

I don´t know what to do. This should catch my name antonio.

var text = "bla bla alsldl bla bla alsldl bla bla alsldl antonio\
bla bla alsldl antonio bla bla alsldl antonio ";

var myName = "antonio";

var hits = [];
hits.push("antonio");
hits[6];

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


#2

I would split text text = text.split(' '); then compare each word with your name. Here is the full working code for that.

var text = "bla bla alsldl bla bla alsldl bla bla alsldl antonio\
bla bla alsldl antonio bla bla alsldl antonio ";

text = text.split(' ');

var myName = "antonio";
var hits = 0;

for(x = 0; x < text.length; x++){
   if(text[x] == myName){
       hits += 1;
   }
}
console.log(hits);

#3

I'm going to go through your code real quick and mark up some stuff. Afterwards I'll show you an alternative and lastly end with the alternative code. I see where you were going with this but unfortunately it was on the wrong track.

hits.push("antonio"); //This line is invalid
hits[6]; //This line has no value

for ( var i = 0; i if(text[i] === "a"){ //For loop is disorganized.
 for ( var j = i; j<(myName.length );i++){ //Postfix is incrementing i instead of j
 return j; //Should be the hits.push(text[j]); code
 }
}
hits.push(text[j]); //Code should be in the second for loop.
console.log(hits); //This line is Correct

For loops are made of 4 distinct parts which are the following:

  • Initialization: Creating a variable,
  • Condition: If the variable in this for loop is producing "true" then execute code,
  • "PostFix": After your codes execution the postfix should increment or decrement your variable here,
  • Execution: This is the code that will be run until the condition for the loop is "false"

for ( Initialization ; Condition ; Increment / Decrement / "PostFix") {
  //Execution
}

So let's see what the corrections should look like!

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

You should also add another "if" statement prior to your second for loop. This way your second for loop doesn't always loop regardless of whether or not it should. That should look like this!

for ( var i = 0 ; i < text.length ; i++)
{
	if (text[i] === myName[0])  { ... }
}

The second for loop should be incrementing it's own variable, not the variable in the for loop before it. So it'll look similar to this:

/*for ( var i = 0 ; i < text.length ; i++)
{
	if (text[i] === myName[0]) 
	{*/
		for(var j = i ; j < (myName.length + i) ; j++)
		{
			hits.push(text[j]); /*Add the hits.push to put anything in
                                            myName into your hits array. This happens
                                            one letter at a time.*/
		}
	/*}
}*/

Finally Let's add an "if" statement to the end of your code to see if your text actually had your name!

/*for ( var i = 0 ; i < text.length ; i++)
{
	if (text[i] === myName[0]) 
	{
		for(var j = i ; j < (myName.length + i) ; j++)
		{
			hits.push(text[j]);
		}
	}
}*/
if (hits === 0){ //if hits has nothing within it.
console.log("Your name wasn't found!"); 
}
else { //otherwise print whatever that's inside hits.
console.log(hits);
}

Let me know if you have any questions and I'll explain a in a little more detail!