Your second "for" loop - I'm so lost!


#1


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


My code runs - I am just really confused about a few this.

I have tried annotating every line - to explain it to myself - and it works - I more have questions from people who understand better than me.

1) why do I need to create the variable j instead of just using i again?
2) because there are 2x FOR loops - do both run - or does the inner FOR (the second one using "j" as variable) run, until the end of the for conditions are met
3) why do I need "(text[i] === myName[0])" ..... is this because I want it to start at the start of something that looks like my name?

Sorry - I know this might be doofus questions, and maybe I'll understand once I have more practice......

Cath


/*jshint multistr:true */

var text = "Hi - my name is Cath. How are you? Do you know \
many people called Cath?";
// variabe "text" is multiline, and a big string

var myName = "Cath"
// variable "myName" is set to a chort string field which will be used later for identification

var hits = []
// variable "hits" is set up - as an empty array - data will be pushed here later

for (var i = 0; i <= text.length; i++) {
// in the for statement, 
// 1: set variable i to zero, 
// 2: run while variable "i" (number) is less than or equal to the .length of the
// variable "text"
// 3: after it runs, increment "i" by 1
// while the "for" runs, do the following ......

    if (text[i] === myName[0]); {
// if the letter retrieved from "text" variable (based on number being "i" 
// for position) is an exact match to myName variable (the very first
// char from "myName"

        for (var j = i; j < i + myName.length; j++) {
// 1: create a variable called "j" which is equal to "i"
// 2: run while "j" is less than "i" plus the length of "myName"
// 3: after running, increment "j" by 1

        hits.push(text[j]);
// in the for statement, when it runs, push into the "hits" array
// the text retrieved from "text" variable, in the field position
// determined by "j"

        }
    }
}

console.log(hits[j]);
// log in the console the text from "hits" array (based on position "j")


#2

Hi microslayer30190

1. JavaScript Variables

JavaScript variables are containers for storing data values.

For example

  • i = 0
  • j = 1

So in this example it did that as an example, it was just saying to make i = j so you can use that for your for loop because i don't think that you can use the same variable for you to compare the two variables.


Does two for loops both run?

Java-script is single threaded so i think it is only one at a time, not sure.


why do I need "(text[i] === myName[0])" .....

Well === means if it is equal value and equal type
For example ("example1" === example2) as you see the not the same so there false because example1 is a string because it has quationmarks but example2 is a variable because it is not defined.


Hope this help's,

Peter.


#3

The 2nd for loop only runs if its condition is met. Otherwise, only the 1st loop executes and nothing gets pushed into the hits array. Try removing the myName variable from the text string on line 1 to test this.

Does this line execute in the Codecademy console?
I've tried it in 2 compilers and it logs undefined to the console.


#4

Thanks so much for everone's help!

I did a funky way of logging everything (below) so that I can see what is being returned at every action -- which helped me understand the program more.....

=====================================================================================

CODE

/*jshint multistr:true */

var text = "Hi - my name is Cath. How are you? Do you know \
many people called Cath? how about Ctahzz?";

var myName = "Cath"
console.log("myName length is: " + myName.length)

var hits = []

for(var i = 0; i< text.length; i++) {
console.log("1st FOR >> I number and letter: " + i + " / " + text[i]);

if (text[i] === "C") {
console.log("If statement triggered - I number v2: " + i)

for(var j = i; j < (myName.length + i); j++) {
console.log("2nd FOR >> J number and letter: " + j + " / " + text[j]);
console.log("2nd FOR >> J myName length: " + myName.length);
console.log("2nd FOR >> I number and letter: " + i + " / " + text[i]);

hits.push(text[j]);
console.log("data in hits: " + hits);
}
}
}

console.log(hits)

=====================================================================================

RESULT:


#5

I am also confused in this,so i tried copying this code and it is giving me an output.

I understood the concept you wanted to define here but the only problem is the output why Am I getting an output with an undefined loop...

Kindly help me out in this.!!
Thanks


#6

I had a go and tried this (to be same as yours or similar)

/*jshint multistr:true */

var text = "My name is ak and this string consist of bulk data which is different from \
myname variable";

var myName = "akshay";

var hits = [];

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

in the console screen I get:

[]
[]
[]
[]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a', 'm', 'e', ' ', 'i', 's', 'a', 'k', ' ', 'a', 'n', 'd', 'a', 'n', 'd', ' ', 't', 'h' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l' ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l',
'a',
'b',
'l',
'e',
undefined,
undefined ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l',
'a',
'b',
'l',
'e',
undefined,
undefined ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l',
'a',
'b',
'l',
'e',
undefined,
undefined ]
[ 'a',
'm',
'e',
' ',
'i',
's',
'a',
'k',
' ',
'a',
'n',
'd',
'a',
'n',
'd',
' ',
't',
'h',
'a',
't',
'a',
' ',
'w',
'h',
'a',
' ',
'w',
'h',
'i',
'c',
'a',
'm',
'e',
' ',
'v',
'a',
'a',
'r',
'i',
'a',
'b',
'l',
'a',
'b',
'l',
'e',
undefined,
undefined ]

if I move the console.log outside ALL the braces I get the same.

I think using the hit[j] is causing issued with "undefined" but also the placement of console.log.

Also because you are telling it how many chars to return, but your full name is not there, I think in my eg, thats why "undefined" is still returned sometimes?

Sorry - I'm very new too.


#7

The output should be different if you place your console.log statement after the last curly brace. If it comes before the last curly brace, you are logging both loops to the console.


#8

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