Code Challenge #12: July 5, 2017

max = 1000000
scores = [874300, 879200, 1172100, 1141800, 933900, 1177200, 1190200,
  1110100, 1158400, 985600, 1047200, 1049100, 1138600, 1170500, 1064500,
  1190000, 1050200, 1090400, 1062800, 1061700, 1218000, 1068000,
  1127700, 1144800, 1195100]

function numberSorter(a, b) {
  return a - b;
}

// --
// Flatten an Array, and compact it.
// Throws out null, undefined
// @return [Array]
// --
function compactAndFlattenArray(array, obj) {
  if (!obj) {
    obj = []
  }

  array.forEach((v) => {
    if (Array.isArray(v)) {
      compactAndFlattenArray(v, obj)
    }

    if (v) {
      obj.push(v)
    }
  })

  return obj
}

// --
// Defensive Version
// --
function scoreSettler1(scores, max) {
  max = Number(max)
  if (max == NaN) {
    throw "Max must be > 0, and a number"
  }

  return compactAndFlattenArray(scores.sort(numberSorter)).filter((v) => {
    return Number(v) != NaN && v < max
  })
}

// --
// Non-Defensive Version
// --
function scoreSettler2(scores, max) {
  return scores.sort(numberSorter).filter((v) => {
    return v < max
  })
}