Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:47:01+00:00 2026-05-26T17:47:01+00:00

My program currently loops through an array and counts the amount of times a

  • 0

My program currently loops through an array and counts the amount of times a pattern occurs in all of them together.

For example: if I should search “at” at prompt I will end up with:

0,1,2,3,4

But really I want to end up with

4,1

As at appears 4 times in array 1 and once in array 2.

This being the amount of times the pattern occurs in the array.

Any advice on how I can achieve this?

Many thanks in advance!

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

var search = prompt('Search?');

function count(web, pattern)
{
    if (pattern)
    {
        var num = 0;
        var result = [];
        for (i = 0; i < web.length; i++)
        {
            var current = web[i];
            var index = current.indexOf(pattern);
            while (index >= 0)
            {
                result[result.length] = num++;
                index = current.indexOf(pattern, index + 1);
            }
        }
        return result;
    }
    else
    {
        return ("Nothing entered!");
    }
}
alert(count(web, search));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:47:02+00:00Added an answer on May 26, 2026 at 5:47 pm
    var web = ["cat fat hat mat", "that the who"];
    
    var search = prompt('Search?');
    
    function count(web, pattern)
    {
        if (pattern)
        {
            var num = 0;
            var result = [];
            for (i = 0; i < web.length; i++)
            {
                var current = web[i];
                var index = 0;
                do{
                    result[i] = num++;
                    index = current.indexOf(pattern, index + 1);
    
                }while (index >= 0)
                num = 0;
            }
            return result;
        }
        else
        {
            return ("Nothing entered!");
        }
    }
    alert(count(web, search));
    

    This works too

    var web = ["cat fat hat mat", "th the who"];
    
    var answer = prompt('Search?');
    var search = new RegExp( answer, "g");
    
    function count(web, pattern)
    {
        var result = [];
        for(var i=0; i< web.length; i++){
            var matches = web[i].match(search) || [];
            result[i] = matches.length;
        }
    
        return result.join(",");
    }
    alert(count(web, search));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a program that has many of those the user SHOULD
I am trying to track visible windows from all currently running processes. My program
Currently I am writing a program that must iterate through and arraylist inside of
Currently my program is in a spot where it both listens for the user
Say I have a command line C program which is currently executing, and I
How can I check the umask of a program which is currently running? [update:
The program that I am currently assigned to has a requirement that I copy
I currently have a small Java program which I would like to run both
I'm currently designing a program that will involve some physics (nothing too fancy, a
I am currently trying to program my first ajax interface using Rails. The application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.