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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:13:06+00:00 2026-05-31T07:13:06+00:00

I wrote a binary search in javascript. Array.prototype.binarySearch = function(find) { var low =

  • 0

I wrote a binary search in javascript.

Array.prototype.binarySearch = function(find) {
  var low = 0, high = this.length - 1,
      i;
  while (low <= high) {
    i = Math.floor((low + high) / 2);
    if (this[i] > find) { low = i; continue; };
    if (this[i] < find) { high = i; continue; };
    return i;
  }
  return null;
}

It fails though to find 5 in my array of integers.

var intArray = [1, 2, 3, 5]

if (intArray.binarySearch(5))
  alert("found!");
else 
  alert("no found!");

Here is a Fiddle.
http://jsfiddle.net/3uPUF/3/

  • 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-31T07:13:08+00:00Added an answer on May 31, 2026 at 7:13 am

    You have the logic backward for changing low and high, if this[i] > find then you want to look between 1 and i-1. If this[i] < find then you want to look between i+1 and the length of the array.

    Try making these changes:

    Array.prototype.binarySearch = function(find) {
      var low = 0, high = this.length - 1,
          i;
      while (low <= high) {
        i = Math.floor((low + high) / 2);
        if (this[i] == find) { return i; }; 
        if (this[i] > find)  { high = i - 1;};
        if (this[i] < find)  { low = i + 1;};
      }
      return null;
    }
    
    var intArray = [1, 2, 3, 5]
    //index of the element in the array or null if not found    
    alert(intArray.binarySearch(5));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function that uses a binary search to look for a specific
I wrote a binary search function as part of a larger program, but it
I wrote the following program for conducting Binary search on an array when the
I am writing a test for a binary search function I wrote. module Tests
Please look at this code: def chop(array, search): lo = 0 high = len(array)
I'm trying to write binary search tree's content to temporary array in order to
This is a past exam paper on binary search trees I am attempting. I
I just wrote this exam, in which there was question: Consider a array of
Is there a library function that performs binary search on a list/tuple and return
In an Open Source program I wrote , I'm reading binary data (written by

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.