or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-9/

Mathematical Expression Calculator with State

A calculator application that maintains state across multiple evaluations, allowing users to define variables and functions that persist between calculations.

Capabilities

Maintains variable state across evaluations

  • Evaluating "x = 5" followed by "x + 3" returns 8 @test
  • Evaluating "a = 10", "b = 20", then "a + b" returns 30 @test
  • Variables persist until explicitly redefined or cleared @test

Supports function definitions

  • Defining "f(x) = x^2" then evaluating "f(4)" returns 16 @test
  • Defining "area(r) = pi * r^2" then evaluating "area(5)" returns approximately 78.54 @test
  • Functions can reference previously defined variables @test

Handles mathematical expressions

  • Evaluating "2 + 3 * 4" returns 14 @test
  • Evaluating "sqrt(16) + 2^3" returns 12 @test

Provides state management

  • A clear() method resets all variables and functions @test
  • Variables and functions can be retrieved individually @test

Implementation

@generates

API

/**
 * Creates a new calculator instance with persistent state.
 */
class Calculator {
  /**
   * Evaluates a mathematical expression or assignment.
   * Supports variable assignments (x = 5), function definitions (f(x) = x^2),
   * and mathematical expressions.
   *
   * @param {string} expression - The expression to evaluate
   * @returns {number|undefined} The result of the evaluation, or undefined for assignments
   */
  evaluate(expression) {
    // IMPLEMENTATION HERE
  }

  /**
   * Clears all stored variables and functions from the calculator state.
   */
  clear() {
    // IMPLEMENTATION HERE
  }

  /**
   * Retrieves the value of a stored variable.
   *
   * @param {string} name - The name of the variable
   * @returns {*} The value of the variable
   */
  get(name) {
    // IMPLEMENTATION HERE
  }
}

module.exports = { Calculator };

Dependencies { .dependencies }

mathjs { .dependency }

Provides mathematical computation and expression parsing support.