or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-5/

Custom Function Calculator

Build a calculator that allows users to define their own mathematical functions using formula notation and then use those functions in subsequent calculations. This is useful for scientific computing, financial analysis, or any application where users need to create reusable custom formulas.

Capabilities

Single-Parameter Function Definition

  • The calculator defines a function "square(x) = x^2", then evaluates "square(5)" to get 25 @test
  • The calculator defines "circleArea(r) = pi * r^2", then evaluates "circleArea(3)" to get approximately 28.27 @test

Multi-Parameter Function Definition

  • The calculator defines "distance(x1, y1, x2, y2) = sqrt((x2-x1)^2 + (y2-y1)^2)", then evaluates "distance(0, 0, 3, 4)" to get 5 @test
  • The calculator defines "quadratic(a, b, c, x) = ax^2 + bx + c", then evaluates "quadratic(1, -3, 2, 2)" to get 0 @test

Using Custom Functions in Expressions

  • The calculator defines "double(x) = 2*x", then evaluates the expression "double(3) + double(5)" to get 16 @test
  • The calculator defines "f(x) = x^2" and "g(x) = 2*x + 1", then evaluates "f(g(2))" to get 25 @test

Functions with Conditionals

  • The calculator defines "absolute(x) = x >= 0 ? x : -x", then evaluates "absolute(-7)" to get 7 and "absolute(7)" to get 7 @test

Implementation

@generates

API

/**
 * A calculator that supports user-defined functions.
 */
class Calculator {
  /**
   * Creates a new Calculator instance.
   */
  constructor() {
    // IMPLEMENTATION HERE
  }

  /**
   * Defines a custom function using formula notation (e.g., "f(x) = x^2").
   *
   * @param {string} functionDefinition - The function definition string
   */
  defineFunction(functionDefinition) {
    // IMPLEMENTATION HERE
  }

  /**
   * Evaluates a mathematical expression, which may use previously defined functions.
   *
   * @param {string} expression - The expression to evaluate
   * @returns {number} The result of evaluating the expression
   */
  evaluate(expression) {
    // IMPLEMENTATION HERE
  }
}

module.exports = { Calculator };

Dependencies { .dependencies }

mathjs { .dependency }

Provides mathematical expression evaluation and parsing capabilities.

@satisfied-by