mgechev / javascript-algorithms

💻 JavaScript implementations of computer science algorithms
https://mgechev.github.io/javascript-algorithms/
MIT License
7.83k stars 1.27k forks source link

Binary search test is wrong #70

Closed jcoder58 closed 9 years ago

jcoder58 commented 9 years ago

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);
 });
mgechev commented 9 years ago

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

jcoder58 commented 9 years ago

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.

mgechev commented 9 years ago

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

mgechev commented 9 years ago

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