My full name doesn't get logged, also I have no Idea the second loop is working


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661#


This is what got logged on my console.

[ 'A' ]
[ 'A', 'y' ]
[ 'A', 'y', 'u' ]
[ 'A', 'y', 'u', 's' ]
[ 'A', 'y', 'u', 's', 'h' ]
[ 'A', 'y', 'u', 's', 'h', ' ' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A', 'y' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A', 'y', 'u' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A', 'y', 'u', 's' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A', 'y', 'u', 's', 'h' ]
[ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A', 'y', 'u', 's', 'h', ' ' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a' ]
[ 'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a',
'A',
'y',
'u',
's',
'h',
' ',
'B',
'a',
'h',
'u',
'g',
'u',
'n',
'a' ]

In the text my name, "Ayush Bahuguna", appears twice, but in console, the first gets logged completely, but the second time it only either logs 'A','y','u'.'s'.'h' or the initial letter 'B' of the last name.

Another thing, why does it get logged vertically?

Also, I have no idea, how is it being able to extract the full name? I mean, there can other words starting with A, how does the second loop differentiate them from my name?

Thanks for being patient with me.


var text="Hey, that's Ayush Bahuguna. Look where Ayush Bahuguna has reached.";
var myName="Ayush Bahuguna";
var hits=[];
for(var i=0; i<text.length; i++){
    if(text[i]==='A') {
        for(var j=i;j<(i+myName.length);j++){
            hits.push(text[j])
            console.log(hits);
        }
    }
}
if(hits.length === 0){
    console.log("Your name wasn't found!");
}
else {
    console.log(hits);
}


#2

Hello, @arcsurfer32640!
First of all, look to your var text. This line occurs twice "Ayush Bahuguna".

Lets try to read loop for line by line closely.

for(var i = 0; i < text.length; i++) { // loop execute a block of code a number of times if condition return true
    if(text[i] === 'A') { 
        for(var j=i;j<(i+myName.length);j++){
            hits.push(text[j])
            console.log(hits);
        }
    }
}

So, loop reads all chars in variable text step by step.

text[0] = 'H'
text[1] = 'e'
text[2] = 'y',
...
text[12] = 'A',
text[13] = 'y'
text[14] = 'u'
text[15] = 's'
text[16] = 'h'
...
text[12] = 'A', -> for(var i = 12; i < text.length; i++) and if(text[12] === 'A') return true here (because text[12] = 'A')
text[13] = 'y'
text[14] = 'u'
text[15] = 's'
text[16] = 'h'
...
text[39] = 'A', -> and here
text[40] = 'y'
text[41] = 'u'
text[42] = 's'
text[43] = 'h'
...
text[65] = '.'

So, in loop we have 2 hits of char 'A'. Now, lets see what you do when text[i] equal to 'A':

for(var j = 12; j < (12 + myName.length); j++) { 
   hits.push(text[j]) // you push in an empty array named hits value 'A')
   console.log(hits); // here you print an array hits every time while j < i + myName.length (BUT WHY???)
} // this code was executed twice: 1st time when i = 12, 2nd time when i = 39

I just want to say:
i = 12, j = 12 => hints = [ 'A' ]
i = 12, j = 13 => hints = [ 'A', 'y' ]
i = 12, j = 14 => hints = [ 'A', 'y', 'u' ]
...
i = 39, j = 39 => hints = [ 'A', 'y', 'u', 's', 'h', ' ', 'B', 'a', 'h', 'u', 'g', 'u', 'n', 'a', 'A' ]
... and so on

Result of for loops is an array of chars. And next piece of code just print all elements of an array hits:

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

Do you find some answers?


#3

The goal of this task is to learn for loop and it's a good exercise. In my practice I use JavaScript String Methods.

1st example:

var text = "Hey, that's Ayush Bahuguna. Look where Ayush Bahuguna has reached.";
var myName = "Ayush Bahuguna";
var hits = [];

var index = text.search(myName);
if (index != -1) {
     for (var i = index; i < (index + myName.length); i++) {
            hits.push(text[i]);
     }
}
else {
     console.log("Your name wasn't found!");
}

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

2nd example:

var text="Hey, that's Ayush Bahuguna. Look where Ayush Bahuguna has reached.";
var myName="Ayush Bahuguna";

console.log( text.substr(text.search(myName), myName.length) );

#4

What if I had some other word starting with A? Would it too get logged? Also, why does it get logged first horizontally and then vertically?


#5

What if I had some other word starting with A? Would it too get logged?

If you add another word starting with A it get logged too.

For example,

var text = "Blah blah blah blah blah blah Adam blah blah blah Anthony blah blah 
Adam blah blah blah blah blah blah blah Adam";

var myName = "Adam";
var hits = [];

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

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

Result: [ 'A', 'd', 'a', 'm', 'A', 'n', 't', 'h', 'A', 'd', 'a', 'm', 'A', 'd', 'a', 'm' ]

I hope, you find answers and understand why we have Adam, Anth (not Anthony), Adam, Adam. If not look to nested for loop:

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

Also, why does it get logged first horizontally and then vertically?

Maybe, problem in your nested for loop where you print result array every time when char added:

for(var j=i;j<(i+myName.length);j++){
   hits.push(text[j])
   console.log(hits); // check your code with/without this line
}

I described this problem in my post #1.


#6

Hi, sorry for bothering you again, but I tried the first example, and it logged my name just once. I understood your code, but can't understand, if my name appears twice in the text, then why didn't it get logged twice?

Thank You


#7

From MDN:

String.prototype.search()

Return value
The index of the first match between the regular expression and the given string; if not found, -1.


#8

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