smallest positive number not in array

by Shawn Wood

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/1.10.0/chai.min.js"></script>
<div id="mocha"></div>

JavaScript

// Configure Mocha and Chai to use BDD-style tests
mocha.setup("bdd");
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();

/** Returns the smallest missing positive number
 * @function 
 * @name solution
 * @param {Object[]} K - Array of numbers
 */
function solution(A) {
  // create a second array for comparison
  let result = [];
  // Set the count to one to deal with negative numbers
  let count = 1;
  //run through all elements of the input array
  for (let i = 0; i < A.length; ++i) {
    // for each number set the respective key in the second array to true
    if (0 <= A[i]) {
      result[A[i]] = true;
    }
  }
	// run through the second array
  for (let i = 1; i <= result.length; ++i) {
    // Return the first key which value comes back as undefined
    if (undefined === result[i]) {
      return i;
    }
  }
  // if no match is found, return count
  return count;
}

// Run tests
describe('[1, 3, 6, 4, 1, 2] returns a 5', function() {
  const arry = [1, 3, 6, 4, 1, 2];
  it('[1, 3, 6, 4, 1, 2] returns a 5', function() {
    expect(solution(arry)).to.equal(5);
  })
});

describe('[1, 2, 3] returns a 4', function() {
  const arry = [1, 2, 3];
  it('[[1, 2, 3] returns a 4', function() {
    expect(solution(arry)).to.equal(4);
  })
});

describe('[-1, -3, -6] returns a 1', function() {
  const arry = [-1, -3, -6];
  it('[-1, -3, -6] returns a 1', function() {
    expect(solution(arry)).to.equal(1);
  })
});
// Run all our tests (only necessary in the browser)
mocha.run();