0

How do you count results within an array?

I am counting the occurrences of a pattern in an array and could like it to return as

[0, 0, 0]

For example with each element of the array representing a search result.

This is what I've done so far... but it seems to only return [object object]...

pages=[
"Lorem Ipsum is simply dummy text of the printing and typesetting industry."
, 
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout", 
"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable." 
]
pattern = prompt("Enter a search string")

    var count = {};

    for(i=0; i<pages.length; i++)
    {
         for(j=0; j<pattern.length; j++)
         {
               if((pages[i].toLowerCase()
                           .indexOf(pattern.toLowerCase()
                                           .charAt([j])))<0)
               {
                  count[i]++;
               }
         }
    }

    alert(count);
6
  • Well, that's because it is an object. Dir it to the console to view its contents: console.dir( count ); Commented Nov 2, 2011 at 21:15
  • Why are you iterating over each character or the search string? Don't you just want to search for the entire pattern? Commented Nov 2, 2011 at 21:28
  • Yes I do and I thought this solution would work best! Commented Nov 2, 2011 at 21:31
  • Is there any way I can improve it? Commented Nov 2, 2011 at 21:35
  • 1
    I'm not sure what you're trying to achieve. Could you update your question with an example (pages + search string + expected result)? Commented Nov 2, 2011 at 21:57

2 Answers 2

1
var count, allCounts = [];

for(i=0; i<pages.length; i++)
{
     count = 0;
     for(j=0; j<pattern.length; j++)
     {
           if((pages[i].toLowerCase()
                       .indexOf(pattern.toLowerCase()
                                       .charAt([j])))<0)
           {
              count++;
           }
     }
     allCounts.push(count);
}
alert('[' + allCounts.join(', ') + ']');

I suggest that you .toLowerCase your pattern once before the start of any of the loops, that would avoid doing the toLowerCase operation i * j times.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you need to move the alert(count) up 2 lines to be like this:

var count = {};

           for(i=0;i<pages.length;i++)
           {
               for(j=0;j<pattern.length;j++)
                   {
                       if((pages[i].toLowerCase().indexOf(pattern.toLowerCase().charAt([j])))<0)
                       {
                           count[i]++;
                       }
                   }
            alert(count[i]); // but note that it will appear multiple times
           }

1 Comment

Alerting within a loop - sounds like fun. Console please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.