DEV Community

Discussion on: Daily Challenge #218 - Possible Sides of a Non-Right Triangle

Collapse
 
empereol profile image
Empereol

TypeScript

/**
 * Return a list of all possible third side lengths (integers) without right
 * trangles.
 *
 * @param sideA Integer
 * @param sideB Integer
 *
 * @throws {TypeError} If either sideA or sideB is not an integer.
 *
 * @example
 * sideLen(1, 1) → [1]
 * sideLen(3, 4) → [2, 3, 4, 6] // 5 is removed as it's a right triangle
 * sideLen(4, 6) → [3, 4, 5, 6, 7, 8, 9]
 */
function sideLen(sideA: number, sideB: number): number[] {
  if (!Number.isInteger(sideA) || !Number.isInteger(sideB)) {
    throw new TypeError('Both provided parameters must be integers');
  }

  const min = Math.abs(sideA - sideB);
  const max = sideA + sideB;
  const hypot = Math.hypot(sideA, sideB);
  const lengths: number[] = [];

  for (let i = min + 1; i < max; i++) {
    if (i !== hypot) {
      lengths.push(i);
    }
  }

  return lengths;
}
Collapse
 
aminnairi profile image
Amin

I think this is the first time I see someone do runtime type checking in TypeScript. Plus, your code helped me understand the goal of the challenge of today. Thanks for that!