Search text for your Name "extended"


#1

After finishing this course "topc name" I was curious how to extend the code for detailed search of your name.
So i came up with the idea to extend the for/"if" loop for 2 more letters.

And that´s my done work. So, why do I post it here? It´s simple I just wanna hear your opinion about it.

Code:

   var text = "asdjJulifhjh hsdJaJuni njkfhsJJkjfJunihs fjkhsdf kjsdh Juri jsdhfkhsjashdfk \
            adsfjhajdsfh adjksfhjksdhfjhdsfJurisjahdfkhasdfjlhj.";
var myName = "Juri";
var hits = []

for(i = 0; i < text.length; i++) {
    if(text[i] === "J") {
        for(var j = i; j < (myName.length + i); j++) {
            if(text[j] === "u") {
                for(var k = j; k < (myName.length + j); k++) {
                    if(text[k] == "r") {
                        for(var g = k - 2; g < (myName.length + (k -2)); g++) {
                            hits.push(text[g]);
                        }
                    }
                }
            }
        }
    }
}
if(hits.length === 0) {
    console.log("Your name wasn't found!");
}
else {
    console.log(hits);
}

#2

Very Precise to the point. Man Nice!


#3

Thank you for your Opinion. I really appreciate it!


#4

A very innovative idea!! congrats! But why do you type '- 2'?


#5

Hi jurzik,

Congrats on making a working program!

I did note: in the program as written, do you think it might run a hair more efficiently if
for(var j = i; j < (myName.length + i); j++)
became
for(var j = i+1; j < (myName.length + i); j++)
and so forth?

If I am doing the readthrough correctly it seems like the for statement above has to run at least twice for each J found before matching a u, etc. (I noticed also that Jurn, Jurp, etc. would also be pushed to your array.)

I notice that your nested if and for statements are all basically the same (if text matches j letter of my name and the next and the next, push it to my array), which reminded me of the intro exercise of this section (console.log five numbers).

That made me wonder: can you find a way to condense your four if-forstatements into a second or thirdfor loop? Just a thought.

Nice work again! :slight_smile:

--frisby


#6

Jules,

I think it "rewinds" back to the beginning of the name so that all four letters of "Juri" are pushed to the array. If there were a Juri at the beginning of the text the effect would be canceled out:

text: Juri blah blah blah
Juri indices: 0, 1, 2, 3

k = r = 2
g = k - 2 = 2 - 2 = 0

Letters get pushed to the array starting from the g position (0, or 'J') until length of myName.length + (k+2) (in this case the length of myName because k+2 evaulates to 0.

However, if the name comes later, the effect would not be canceled out:

text: aJuri blah blah blah
Juri indices: 1, 2, 3, 4

k = r = 3
g = k - 3 = 3 - 2 = 1

Letters get pushed to the array starting from the g position (1, or 'J') until length of myName.length + (k+2) which would now be myName.length + 1.

Without the additions of the - 2, instances of Juri that start anywhere but from the 0th position of the text would be ignored.

frisby


#7

Thanks, I really appreciate your support!


#8

No problem @julesdebruin, good luck with the rest of the course! :slight_smile: