Two Sum: Find Indices That Add Up to Target

Find two array indices whose values sum to a target in one pass using a hash map, with validation for non-numeric input.

JavascriptData Integrity

Code

snippet
export function twoSum(numbers, target) {
  if (!Array.isArray(numbers)) throw new TypeError("numbers must be an array");
  if (!Number.isFinite(target)) throw new TypeError("target must be a finite number");
  const seen = new Map();
  for (let index = 0; index < numbers.length; index += 1) {
    const value = numbers[index];
    if (!Number.isFinite(value)) throw new TypeError("numbers must be finite at index " + index);
    const complement = target - value;
    if (seen.has(complement)) return [seen.get(complement), index];
    seen.set(value, index);
  }
  return null;
}