fr_muses
fr_muses

Reputation: 1473

Javascript for loop & arrays

I was running the following code to get the relevant output which displays the alphabets of the word Eric.

/*jshint multistr:true */

text = "My name is Eric. Eric lives in New York";
var myName = "Eric";
var hits = [];

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

I am able to obtain the desired output which is [ 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c' ]

However, when I enter "l" in line 8 of my code I get the following output: [ 'l', 'i', 'v', 'e', 's', ' ', 'i' ]

As per my understanding, the code should return a message Your name wasn't found!.

Instead it is still processing the characters after the letter l and returning it as an output.

How can I refine my code in a better way to ensure that only the characters in the string Eric are searched and returned as an output whereas any other characters will be rejected?

Upvotes: -1

Views: 888

Answers (4)

if Eric is completely matched then only it puts it in the array.

eg(Eric is in array but Eriic is not)

Working Demo http://jsfiddle.net/9UcBS/1

text = "My name is Eric. Eric lives in New York Eriic";
var myName = "Eric";
var hits = [];

var pos = text.indexOf(myName);
while (pos > -1) {
    for (var j = pos; j < (myName.length + pos); j++) {
        hits.push(text[j]);
    }
    pos = text.indexOf(myName, pos + 1);
}
(hits.length === 0) ? console.log("Your name wasn't found!") : console.log(hits);

Upvotes: 2

beatgammit
beatgammit

Reputation: 20205

It would be a ton easier to use a regex in this case:

var name = "Eric";
var text = "My name is Eric. Eric lives in New York";
var hits = [];
var regexp = new RegExp(name, "g");
var match;
while (match = regexp.exec(text)) {
    hits.push(match[0]);
}
console.log(hits);

When using exec with the "g" flag, every call advances the place in the string. So on the first iteration, you'll get the first Eric, then the second will find the next, and so on. For more information, see mdn.

After this loop, hits will be an array of the string "Eric". If you wanted character arrays like you had before, you could use a simple hack:

while (match = regexp.exec(text)) {
    hits = hits.concat([].slice.call(match[0]));
}

Or if you wanted to use straight for loops:

while (match = regexp.exec(text)) {
    for (var i = 0; i < match[0].length; i++) {
        hits.push(match[0][i]);
    }
}

Upvotes: 1

sthede
sthede

Reputation: 928

A good example of using indexOf() to find occurances of strings is located here: https://stackoverflow.com/a/3410557/2577572

The result of the example linked above would give you an array of locations of the string you are searching for. If the array has no length after calling the function detailed above, then your name was not found.

I don't think you need to actually create an array of letters made from the search string, because according to your example, they would be the same as the search string every time.

Upvotes: 0

User not found
User not found

Reputation: 2139

Strings are not character arrays. To get a character in a string from its position, you need to use the charAt method, like this:

if ( text.charAt(i) == "E" ) {
    //...
}

Or even better:

if ( text.charAt(i) == myName.charAt(0) ) {
    //...
}

Upvotes: 0

Related Questions