0

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?

fr_muses
  • 1,475
  • 2
  • 10
  • 10
  • 1
    Why should it say that the name isn't found? There's nothing in your code that compares `myName` with the text. All it does is use `myName.length` as part of the limit of a `for` loop, but it doesn't do anything with the characters in `myName`. – Barmar Jul 20 '13 at 05:46
  • Why not use `text.indexOf(myName)` if you want to know if the name is found in the text? – Barmar Jul 20 '13 at 05:51
  • I've tested your code and it doesn't behave the way you say it does. – NPE Jul 20 '13 at 05:56
  • Could you explain the purpose of your code? Perhaps there's a much simpler way of accomplishing what you want. – beatgammit Jul 20 '13 at 06:06

4 Answers4

2

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);
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

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]);
    }
}
beatgammit
  • 19,817
  • 19
  • 86
  • 129
0

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) ) {
    //...
}
User not found
  • 2,129
  • 2
  • 15
  • 20
0

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.

Community
  • 1
  • 1
sthede
  • 914
  • 1
  • 8
  • 27