1

I'm trying to loop through an array to check for a specific pattern but keep getting no output afterwards. Not sure what I've done wrong! I would appreciate any help!

I am testing for the pattern at or hat.

sample = ["cat fat hat mat", "that the who"]
searchTerm = prompt("Testing?");

function count(sample, searchTerm) 
{

    for (i=0;i<sample.length;i++)
    {
    if (sample[i].indexOf(searchTerm) == -1) 
    {
        return 0;
    }
    return count(sample.substring(sample.indexOf(searchTerm) + searchTerm.length), searchTerm) + 1;
    }
}

alert(count(sample, searchTerm));

Rehashed code

search = ["cat fat hat mat", "that the who"];

var pattern = prompt('Search?'); 

function count(sample, searchTerm) 
{
    var count, i;
    count = 0;
    for (i=0; i < sample.length; i++)
    {
        if (sample[i].indexOf(searchTerm) !== -1) 
        {
            count++;
        }
    }
    return count;
}

count(search, pattern);

I've redone everything and it still gives no output.

methuselah
  • 12,766
  • 47
  • 165
  • 315

2 Answers2

2

You don't need to use recursion here, just iterate through the array once counting if the search term matches.

function count(sample, searchTerm) 
{
    var count, i;
    count = 0;
    for (i=0; i < sample.length; i++)
    {
        if (sample[i].indexOf(searchTerm) !== -1) 
        {
            count++;
        }
    }
    return count;
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
a'r
  • 35,921
  • 7
  • 66
  • 67
  • What if there are multiple matches of a specific pattern - would it count it all? – methuselah Nov 04 '11 at 16:08
  • No, this only counts the number of elements in the array that contain the searchTerm. – a'r Nov 04 '11 at 16:09
  • How would I go about counting the amount of patterns within each array as I did here http://jsfiddle.net/CnREL/ – methuselah Nov 04 '11 at 16:12
  • Take a look at: [Count number of matches of a regex in Javascript](http://stackoverflow.com/questions/1072765/count-number-of-matches-of-a-regex-in-javascript) – a'r Nov 04 '11 at 16:16
2

There are a couple of problems with this code. The most immediate one is you are calling substring on an array and not a string.

return count(sample.substring ...

Likely you meant to say

return count(sample[i].substring ...

The second issue though is that you need to divide the logic up a bit. You need to divide it up into sections that count the occurrences in a word and that which iterates through the array. Today they are intertwined and results in odd behavior because you end up passing non-arrays to places expecting arrays

function count(sample, searchTerm) {
  var num = 0;
  for (i=0;i<sample.length;i++) {
    var current = sample[i];
    var index = current.indexOf(searchTerm);
    while (index >= 0) {
      num++;
      index = current.indexOf(searchTerm, index + 1);
    }
  }
  return num;
}

Working Fiddle: http://jsfiddle.net/wrNbL/

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I managed to get something done here [link(http://jsfiddle.net/CnREL/) but when I wrapped it in a for loop it horribly failed! – methuselah Nov 04 '11 at 16:14
  • @jeansymolanza check the fiddle link i just posted to my answer. It has a working sample – JaredPar Nov 04 '11 at 16:15