Search Test for Your Name doesn't make sense


#1

/*jshint multistr:true */

var text = "Non-offensive string for an apparently delicate community? But still using monkey, assuming that's ok. A monkey can help people lighten up";
var myName = "monkey";
var hits = [];

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

}

#2

There is the problem. myName, 'monkey' is only six characters in length. Your inner loop adds 1 to myName.length then iterates over that range. The result is an attempt to push an element that does not existed, hence, undefined.

The code is wrong, just the same. It should read, i + myName.length and it should push text[j] to the hits array.

You could also show some respect and use less juvenile language. Are you 11, or something?


#3

Just the same, when you alter the code to the following:

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

it still doesn't work and pass the eval. So there you go, you know? Have a good one pal!


#4

For good reason. It still contains errors in the logic so isn't creating the array that the SCT expects.

['m', 'o', 'n', 'k', 'e', 'y', ...]

Your array will look like,

[undefined, undefined, undefined, ...]

        for (j = i; j < i + myName.length; j++){
            hits.push(text[j]);
            }
        }

#5

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