or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-2/

Triangle Analyzer

Build a triangle analyzer that calculates various properties of triangles using trigonometric functions.

Requirements

Implement a triangle analysis system that:

  • Accepts a triangle defined by either sides (SSS), two sides and an angle (SAS), or one side and two angles (ASA)
  • Computes all missing sides and angles
  • Calculates the triangle's area
  • Determines if the triangle is valid
  • Handles angles in both degrees and radians
  • Returns results with appropriate precision

The system should validate inputs and handle edge cases such as impossible triangles.

Implementation

@generates

API

/**
 * Analyzes a triangle and computes all properties.
 *
 * @param {Object} input - Triangle definition
 * @param {Object} input.sides - Triangle sides (optional)
 * @param {number} input.sides.a - Length of side a
 * @param {number} input.sides.b - Length of side b
 * @param {number} input.sides.c - Length of side c
 * @param {Object} input.angles - Triangle angles (optional)
 * @param {number} input.angles.A - Angle A (opposite to side a)
 * @param {number} input.angles.B - Angle B (opposite to side b)
 * @param {number} input.angles.C - Angle C (opposite to side c)
 * @param {string} input.angleUnit - Unit for angles: 'degrees' or 'radians' (default: 'degrees')
 * @returns {Object} Triangle properties
 * @returns {Object} return.sides - All three sides {a, b, c}
 * @returns {Object} return.angles - All three angles in the input unit {A, B, C}
 * @returns {number} return.area - Triangle area
 * @returns {boolean} return.valid - Whether the triangle is valid
 * @throws {Error} If insufficient data provided to determine the triangle
 */
function analyzeTriangle(input);

module.exports = { analyzeTriangle };

Test Cases

SSS Triangle (Three Sides)

  • Given sides a=3, b=4, c=5 in degrees mode, returns angles approximately A=37°, B=53°, C=90°, area=6, and valid=true @test

SAS Triangle (Two Sides and Included Angle)

  • Given sides a=5, b=7, angle C=60° in degrees mode, computes the third side c≈6.245 and remaining angles @test

ASA Triangle (Two Angles and One Side)

  • Given angles A=30°, B=60°, side c=10 in degrees mode, computes sides a=5 and b≈8.66 and angle C=90° @test

Radian Mode

  • Given sides a=1, b=1, angle C in radians (π/3), correctly computes the triangle with angles in radians @test

Invalid Triangle

  • Given sides a=1, b=2, c=10, returns valid=false because the triangle inequality is violated @test

Angle from Two Points

  • Given two points defining a line from origin, computes the angle using atan2: points (1,1) gives 45° in degrees mode @test

Dependencies { .dependencies }

mathjs { .dependency }

Provides trigonometric and inverse trigonometric functions for triangle calculations.