Just finished search text for your name lesson


#1

Hi everyone,

So I just finished the 'search text for your name lesson and I'm looking to complete the bonus exercise where I change the code so it knows the difference between Eddie and Erick. I started my code with an easy short name 'tom'. To try and experiment a little. Am I on the right track? If I keep getting the code to loop after 'checking' each letter like I have done for the first two, is that a good way of completing this task?

Any hints are appreciated!


/*jshint multistr:true */
var text = "Hey, how are you \
            doing? My name is tom."
            
var myName = "tom"

var hits = []

for (var i = 0; i < text.length; i=i+1) {
    if (text[i] ==="t"){
        for (var j = i; j < i + 1; j=j+1){
            
            }
            if(text[j] ==="o"){
                for (var k = i; k < i + 2; k=k+1){
                    hits.push(text[k]);
                    }
                    if (text[k] ==="m"){
                        for (var l = i; l < i + 3; l=l+1){
                            hits.push(text[l]);
                            }
                        }
                }
        
        }

}

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


#2

Can you help me understand the second for loop? Why are we continuing as long as j is less than i + 1? Why are we incrementing j by j+1?


#3

Well I actually got this to work as I wanted last night. The code can now tell the difference between the name "Tom" and "Tim". Here's the code.


/*jshint multistr:true */
var text = "Hey, how are you \
            doing? My name is tom."
            
var myName = "tom"

var hits = []

for (var i = 0; i < text.length; i=i+1) {
    if (text[i] ==="t"){
        for (var j = i; j < i + 1; j=j+1){
            
            }
            if(text[j] ==="o"){
                for (var k = i; k < i + 2; k=k+1){
                    
                    }
                    if (text[k] ==="m"){
                        for (var l = i; l < i + 3; l=l+1){
                            hits.push(text[l]);
                            }
                        }
                }
        
        }

}

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

Can you help me understand the second for loop? Why are we continuing as long as j is less than i + 1? Why are we incrementing j by j+1?

Ok, well with this code the first for loop tells the computer to loop through every character in the text block and stop once we reach the text.length. This variable is i and starts at 0.

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

Next is, if variable i = "t" we start another loop

if (text[i] ==="t"){

Next we give a new variable j which starts at where i found the letter t and loops till it is one letter less than i which is increasing by 1 each loop. Essentially it goes to the next letter.

for (var j = i; j < i + 1; j=j+1){

Next is if variable j is "o" we start another loop. This time we give a new variable k and loop until it is two letters ahead of i. So it goes two letters ahead of t.

We continue the process until we have all three letters then push these letters to the array

hits.push(text[l]);

At least this is my understanding of it. I'm a newbie so I might be off with my logic


#4

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