mgechev/javascript-algorithms

Binary search test is wrong

jcoder58 opened this issue · 4 comments

This test from searching/ binarysearch.spec.js has two issues:

it('should find the eleent in position arr.length', function () {
    expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
 });
  • There can never be an element in position arr.length
  • The value found parameter should be 8 instead of 1, assuming its testing for the last element.

You mean the test description is incorrect? The code looks fine to me.

The code is testing the wrong thing. Here's the first two tests:

 it('should find the element at position 0 ', function () {
    expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
 });

  it('should find the eleent in position arr.length', function () {
    expect(binarySearch([1, 2, 3, 4, 6, 8],  1)).toBe(0);
  });

The call to binarySeach has identical parameters for both tests, even though the comments say different things. So the second test either needs to be eliminated, as it tests the same as the first, or it needs to be changed to match the comment.

Here is the fix I did 12-13 mins ago.

@jcoder58 thanks for the issue! I didn't notice that previously 😄