← run

ts-07-mathjs-evaluate

1.000
7/7 tests· math

Proposed solution

```ts
import { compile } from "mathjs";

export function evaluateAll(expr: string, scopes: Record<string, number>[]): number[] {
  if (scopes.length === 0) {
    return [];
  }
  
  const compiled = compile(expr);
  return scopes.map(scope => compiled.evaluate(scope) as number);
}
```

Test output (stdout)

TAP version 13
# Subtest: evaluates with multiple variables across scopes
ok 1 - evaluates with multiple variables across scopes
  ---
  duration_ms: 1.620239
  type: 'test'
  ...
# Subtest: preserves scope order
ok 2 - preserves scope order
  ---
  duration_ms: 0.186291
  type: 'test'
  ...
# Subtest: uses built-in functions
ok 3 - uses built-in functions
  ---
  duration_ms: 0.473873
  type: 'test'
  ...
# Subtest: empty scopes returns empty array
ok 4 - empty scopes returns empty array
  ---
  duration_ms: 0.04956
  type: 'test'
  ...
# Subtest: single scope
ok 5 - single scope
  ---
  duration_ms: 0.109081
  type: 'test'
  ...
# Subtest: result is a number array
ok 6 - result is a number array
  ---
  duration_ms: 0.115311
  type: 'test'
  ...
# Subtest: compiles once and reuses for differing variable values
ok 7 - compiles once and reuses for differing variable values
  ---
  duration_ms: 0.110551
  type: 'test'
  ...
1..7
# tests 7
# suites 0
# pass 7
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 391.362348
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.