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 8199753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:14:10+00:00 2026-06-07T06:14:10+00:00

How would I write this code without all the duplication // I have a

  • 0

How would I write this code without all the duplication

    // I have a loop which decrements m each time
    // set m to the starting point
    m = mid
    // set f to a calculated array value
    f = dict[l].substr( l * --m, l )
    while (f.substr(0,x) === word && (!limit || matches.length < limit)){
        matches.push(f);
        // same as what was defined outside the while loop
        // which seems to me like unnecessary repetition
        f = dict[l].substr( l * --m, l )
    }
    // then I repeat it all, but incrementing m
    // reset m to the starting point
    m = mid
    f = dict[l].substr( l * m++, l )
    while (f.substr(0,x) === word && (!limit || matches.length < limit)){
        matches.push(f);
        f = dict[l].substr( l * m++, l )
    }

There are two parts…

  1. each block contains a repeating f = ... part
  2. the blocks are repeated, only changing the increment/decrement of m

EDIT: What the code does…

mid represents an arbitrary entry point into an alphabetically sorted list of fixed length words with no separators. I am aiming to list all the words that match a set prefix, so must find all the words backwards of the arbitrary mid point (as entered by binary search method) and also forwards.


Edit: Further details…

dictionary looks like this:

dict = [
  "", // offset other values to equal word length
  "ai",    // all length 1
  "inatonuptogo",    // all length 2
  "catcrydogendgodhamhathit",    // all length 3
  "foodhackhallhandhardhatehatshavehellridewood" // all length 4
]

l is word length of searched words, so dict[l] is one row of words from the dictionary, of length l.

I am modifying John Resig’s binary search method so that it will match a prefix rather than a whole word, and return a set of results, rather than a truthy value. I am also putting a limit in there, as I will be using this for an autocomplete function which only requires a few returned values, not all matches.

  • 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-06-07T06:14:11+00:00Added an answer on June 7, 2026 at 6:14 am

    You might pull the loops into a function, since aside from the increment/decrement, they are identical:

    function helperFunction(m, f, l, mid, dict, matches, limit, increment)
    {
        m = mid;
    
        if (increment)
            f = dict[l].substr(l * m++, l);
        else f = dict[l].substr(l * --m, l);
    
        while (f.substr(0, x) === word && (!limit || matches.length < limit))
        {
            matches.push(f);
            f = dict[l].substr(l * m, l);
    
            if (increment)
                f = dict[l].substr(l * m++, l);
            else f = dict[l].substr(l * --m, l);
        }
    }
    

    I’ve included all the variables in the argument list since their scope is unclear.

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

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
This is as far as I've gotten. What code would I write to save
I have a thread safe observable collection replacement which I would like to write
Which is preferable? Assume this code before each: byte[] data = Encoding.ASCII.GetBytes(Test Packet Message);
what would be the shortest way to write this? if(strpos($haystack, $needle)!==false){ $len = strpos($haystack,
I would like to know how to write this inside my hittest if statement:
Not sure this is possible, but looking to write a script that would return
I'm looking for ways to write functions like get_profile(js) but without all the ugly
Back in VB6, I wrote a few functions that would let me code without
Not that I would ever write the code like the following in my professional

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.