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

  • Home
  • SEARCH
  • 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 6796461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:29:32+00:00 2026-05-26T18:29:32+00:00

Hi I am really struggling making code for a function that ‘returns an array

  • 0

Hi I am really struggling making code for a function that ‘returns an array of indexes from an already populated array containing strings, when it matches the search term’.
So if the search term matches a word or characters in the element of the array regardless of its order, it should return the indexes in which those words are apparent in the array. Not using jquery grep feature.
Here is some code to illustrate what I mean.

array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"]

function locateSearch(array_test,searchTerm){ 
var myArray = [];

for(i=0;i<array_test.length;i++){ 
if(...) // what should i be testing here, this is where i have been going wrong... 

myArray[myArray.length] = i; 
} 
 return myArray;  
}

document.write(locateSearch,'ot') //this should return indexes [0,1,2,3]

Sorry If I have not explained this so well, appreciate your help. Thank you.

  • 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-26T18:29:32+00:00Added an answer on May 26, 2026 at 6:29 pm
    array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"];
    
    function locateSearch(array_test,searchTerm){
        searchTerm = searchTerm.toLowerCase();
        var myArray = [];
        for(var i = 0; i < array_test.length; i++){
            for(var j = 0; j < searchTerm.length; j++) {
                if(array_test[i].toLowerCase().indexOf(searchTerm[j]) >= 0) {
                    myArray.push(i);
                    break;
                }
            }
        }
        return myArray;  
    }
    
    alert(locateSearch(array_test, 'To').toString());
    

    Also see this jsfiddle.

    === UPDATE ===

    If every char has to be in the same string:

    array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"];
    
    function locateSearch(array_test,searchTerm){
        searchTerm = searchTerm.toLowerCase();
        var myArray = [];
        var bFound;
        for(var i = 0; i < array_test.length; i++){
            bFound = true;
            for(var j = 0; j < searchTerm.length; j++) {
                if(array_test[i].toLowerCase().indexOf(searchTerm[j]) == -1) {
                    bFound = false;
                    break;
                }
            }
            if (bFound) {
                myArray.push(i);
            }
        }
        return myArray;  
    }
    
    alert(locateSearch(array_test, 'es').toString());
    

    Also see my updated jsfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really struggling to understand why, when I change my code to use
I'm new to LaTeX and I must say that I am really struggling with
i am really struggling to load some numeric floating point data from a file
Really struggling to figure out extending the immutable Set with a class that will
I'm really struggling trying to find something that actually works for getting the basics
So I have been really struggling to grasp the concept, but I am making
Really struggling with this one: I have an existing foreach, containing an if loop
I'm really struggling with a MySQL query that I'm really hoping someone can help
I'm really struggling with grasping how to effectively use FasterCSV to accomplish what I
OK i am really struggling with this! I know had to add an image

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.