"Victory!" page: Find only exact match for myName



I am working on the "Victory!" page.
However, I couldn't complete the task of only finds exact matches for my name.

The outcome of my below script is:-
[ 'j', 'a', 'n', 'e', 't', 'j', 'n', 'e', 't', 'j', 'a', 'n', 'e', 't' ]

However, according to the instruction, i need to have
[ 'j', 'a', 'n', 'e', 't', 'j', 'a', 'n', 'e', 't' ] because i have typed "jpnet" which doesn't match myName.

Can anyone help? Thanks a lot!

Here is my script

var text = "janet is a girl. jpnet is hong kong people.\
janet is working on code academy."

var myName = "janet"

var hits = []

for( var i=0; i<text.length +1; i++){
if(text[i]===myName[0]) {
  for (var j=0; j<myName.length; j++){
      if(text[i+j] == myName[j]){

if(hits === []){
  //console.log("your name doesn't exist")


I think for this exercise you are supposed to make a simple name finder program. I'm pretty sure you are never taught how to skip a word if it starts with the first letter of your name.

But another point is that your code will only print the first word in the variable text instead of your name. Ill show you how to fix that though.

The problem lies here. This is printing each letter of the first word in text. If you remove the first janet it will print is the letters "i" and "s" a couple of times. I will go through this piece of code.

Not sure why there is a +1 after text.length. All you want to do is go through all the letter in text with this for.

Your here need to If we find the first letter of your name, add characters up to the length of my name to the array. So lastly you want to make j = i as you want to be using the loop above in your second for. Then go through the length of your name plus i. Then remove your if statement as there is no need. Look at my code below as reference.

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

Let me know if that solved your problem :slight_smile:


Hey. So this can be a little confusing but I quickly wrote up something and commented as I went to better explain a process in which you could take to make this work.

If you want the code without the comments, you can get it from here: gist

One thing this code won't do is ignore case-specificity. But anyway. Here it is.

/*jshint multistr:true */

var text = "Hello. My name is Angus. \
            Also known as Gus, but I prefer just Angus";

var myName = "Angus";

var hits = [];

// We enter the text string loop
for(i = 0; i < text.length; i++) {
    // If we locate the first character in our name we begin further iterations
    if(text[i] === myName[0]) {
        // depo is used to temporarily store our to-be matching name
        // depo will also serve as an index counter later
        depo = [];
        // This is the part where we iterate over the text string for the amount of
        // letters in our name. 
        for(k = i; k < (i + myName.length); k++) {
            * If we find a matching character we should push it to our temporary array
            * We also leverage depo.length as a way to keep track of our place in                  
            * myName string index
            if(text[k] === myName[depo.length]) {
            * else: if we don't find a match then we should break out of this loop 
            * entirely because there's no point if one character doesn't match - It  
            * means what we are testing for is not ever going to match 100%
            } else {
        * If the length of our temporary array is the same as our name string 
        * then that means we successfully found a match for all the letters in our 
        * name. Hence we can now push out temporary array to the main hits array and
        * count that as a successful match.
        if(depo.length === myName.length) {
            for(l = 0; l < depo.length; l++) {
if(hits.length === 0) {
    console.log("Your name wasn't found!");
} else {

I just wanted to add: Of course there are much more time-saving, simpler ways to go about this. For example, we can use hits.push.apply to join two arrays together. This would make it pointless to loop over our depo array at the end and push single indices. But I decided to demonstrate a more manual approach in some regards to better illustrate what is happening.

Hope that helps.

Slightly Advance Question about Search Text
Slightly Advance Question about Search Text
Slightly Advance Question about Search Text

Thanks a lot for helping!!


Thanks a lot! it works perfect!
It is smart to make a temporary array and whenever this array's length matches with myName's length, it pushed to hits and then loop again to contain another "Angus"!


Can Someone explain to me what does the if(text[i+j]===myName[0]) means


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