var _ = require('lodash')
var findMissingNumberBinarySearch = require('./findMissingNumberBinarySearch')

// Takes a list of schedules and finds the first open year.
// If they go [2012, 2013, 2015] findFirstAvailableYear will
// return 2014. If it goes [2013, 2014, 2015] findFirstAvailableYear
// will return 2016. Etc.

function findFirstAvailableSemester(schedules, forYear) {
	var lastSemester = 5

	var semesters =
		_(schedules)
			.filter({year: forYear})
			.sortBy('semester')
			.pluck('semester')
			.uniq()
			.value()

	var missingNo = findMissingNumberBinarySearch(semesters)
	if (missingNo != -1) {
		return missingNo
	}

	return _.max(semesters) + 1
}

module.exports = findFirstAvailableSemester

@@ -378,31 +378,8 @@
 ) %7B%0A
-%09var lastSemester = 5%0A%0A
 %09var
@@ -503,16 +503,98 @@
 alue()%0A%0A
+%09// stick a 0 at the front so findBinary will start from 1%0A%09semesters.unshift(0)%0A%0A
 %09var mis

Ensure that findFirstAvailableSemester's array starts at 0