5/7 for loop?


#1

/*jshint multistr:true */
//for(var i = 0; i < 5; i ++){
// console.log("I know someone called " + names [i]);
//}

text = "Neo (born as Thomas A. Anderson, also known as The One) is a fictional character in The Matrix franchise. He was portrayed by Keanu Reeves in The Matrix Trilogy, as well\ as having a cameo in The Animatrix short film Kid's Story. Andrew Bowen provided Neo's voice in The Matrix: Path of Neo."
myName = "Neo"
hits = []
for(var i = 0; i > text.length; i ++){
if (text[i] === "N"){
// console.log(hits.push(myName[i]))
for(var j = i; i < i+myName.length; i++){

hits.push(text[j])
}
}
}
console.log( hits.push(text[j]))

It says I accomplished the objective but the console is outputting the character "1"

I'm afraid I do not understand the purpose for this code. I know what a for loop is because it ticks for when the first condition is valid, but I do not understand what pushing to an array is. The console.log( hits.push(text[j])) I have no clue what that does or why its outputting 1


#2

push appends a value at the end of the array

push and pop are commonly used names for adding and removing at the end of a queue or similar

The return value of push is the new length of the array and if you take a look at what your console.log's..


#3

@jstank,
Have a read through
= http://discuss.codecademy.com/t/5-your-second-for-loop/13373/2


#4

/*jshint multistr:true */
//for(var i = 0; i < 5; i ++){
// console.log("I know someone called " + names [i]);
//}

1 text = "Neo born"
2 myName = "Neo"
3 hits = []
4
5for (var i = 0; i < text.length; i++) {
6 // console.log("i= " + i);
7 if (text[i] === "N") {
8
9 for (var j = i; j < (myName.length + i); j++) {
10 hits.push(text[j]);
11 }
12
13 }
14}
15 console.log(hits);

Ok I finally got it to output properly. Now the question is, "Did I understand what I just typed?"

So the way I understand it is that
1. define text as a "string"
2. define myName as a "string"
3. define hits as empty array
5. the computer is going to repeat a loop for when the variable i is from zero and stop when it is the text.length in increments of one. During this operation on every tick the program will check if at each character there is a letter n.
9-10. Starting at the letter N and for the length of myName in increments of one, the computer will place the variable j into the array hits[].
14. the computer prints the array hits[].

I'm having trouble explaining how the second for loop puts the actual letter into the array hits[].

It also doesn't work very well because if I type Nathan into the name box, it just prints Nat into the array!

Is that supposed to happen or am I doing something else wrong.


#5

5 . the computer is going to repeat a loop for when the variable i is from zero and stop when it is the text.length in increments of one. During this operation on every tick the program will check if at each character i of text IF it is a letter N.

9-10. Starting at the letter N and for the length of myName in increments of one, the computer will place the value of text[j] into the array hits[].


#6

aww ok, I think I got it... for now!