r1ckn0rr15
r1ckn0rr15

Reputation: 41

Please help me understand the logic in this 'for loop'

I am currently on Code Academy having fun learning. They have introduced push. and nested for loops pretty quickly without a lot of initial info. I THINK I understand the logic somewhat and would like to see if someone can help break it down...

var text = "Here is a string with the name rick sometimes not always rick   and sometimes rick";
//assigning text string to variable text  

var myName = "rick";  
//assigning rick to variable myName  
var hits = [];  
// assinging an empty array to variable hits  
for (var i = 0; i < text.length; i++); {  
    //for loop i = 0, and as long as i = less than the length of entire text  string keep incrementing 1
    if (text[i] === "r") {  
        //while looping through text when you find an "r" enter second for loop  
        for(var j = i; j < (i+ myName.length); j++){  
            //J takes the value of i at this point and it should be 0 and should   increment 4 steps as myName = 4 characters

            hits.push(text[j]);  
            //this push statement should add each letter of my name to the hits array  
        }  
    }  
}  

At this time my code does not work. I placed a console.log under the first for loop and it just prints 84. "console.log("I= " +I)" I understand this is pretty n00b but I really want to follow the logic and understand what is happening. Am I close?

Upvotes: 0

Views: 120

Answers (3)

ScottCollier
ScottCollier

Reputation: 373

You have a ";" after the for that should not be there. Also, you are still limited by the length of you string. So, you for statement becomes:

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

For example, if you had a string that was 82 characters long and you found an 'r' at the end of your text (i = 81), your original loop would set j to 81 and then try to check for characters in your string for indexes 82, 83, 84, and 85 which do not exist and would cause an error. Adding the addition check of i < text.length makes sure you do not try to check items that don't exists.

I don't know how far you are in your course. Just in case, the '&&' is an AND so j has to be less than (i + myName.length) AND also less than text.length.

Here is a JSFiddle of it working (http://jsfiddle.net/onlinespaces/xj39974c/1/)

Using JSFiddle is a great tool for things like this.

Good luck!

Upvotes: 0

seba
seba

Reputation: 419

You are really close.

You just have one problem:

  • The ";" after the for. It just goes to the end of the "for" loop doing nothing.

Take it out and it will work!!

Another thing you may like is to add to the list "hits" all the word that follows the 'r' character.

You should do:

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

Also, be careful that i+myName.length is still in the limit of text.length (that is, i+myName.length < text.length)

Hope it helps!

Upvotes: 1

jcc273
jcc273

Reputation: 119

Your code is pretty close. You accidentally put a semicolon after your first for loop:

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

This basically makes a 1 line block, same as this:

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

Take that semicolon out and it should do what you want : ). although if your intent is to only push the ricks in then you need to check for more than just "r" you need to validate that each character after is also equal to rick.

Upvotes: 0

Related Questions