5 kyu

No 'Math' expression evaluator

Description:

The Math Department of your favorite college have requested us to write an expression evaluater.

While we can present them with a number of these, they were not satisfied as they really need the full power of the JavaScript Math object.

Additionally they so often use PI^2, cos(x)^2 and sin(x)^2 that they request the solution to implement these (as PI2, cos2 and sin2) Also, they have grown excessively tired of writing "Math." in front of all. So this must NOT be required!

Aside from their own secret tests, they have provided us with a number of tests that should evaluate within normal JavaScript precision limits.

Some expressions may contain variables. Values for these are handed over in a 'context' object and must be applied during evaluation. Use of the context object is optional for the user.

'expr' input will be valid mathematical expressions (maybe including variables of same format as JavaScript identifiers).

The result will always be a Number.

Example expressions are:

evaluate("2*PI2") == 19.739208802178716
evaluate("cos2(v) + sin2(v)", {v:5}) == 1 (almost)
evaluate("(a+b+c)/n", {a:3,b:4,c:5,n:6}) == 2

Algorithms

Stats:

CreatedJan 8, 2014
PublishedJan 13, 2014
Warriors Trained429
Total Skips156
Total Code Submissions1921
Total Times Completed141
JavaScript Completions141
Total Stars24
% of votes with a positive feedback rating80% of 47
Total "Very Satisfied" Votes34
Total "Somewhat Satisfied" Votes7
Total "Not Satisfied" Votes4
Total Rank Assessments9
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • christianhammer Avatar
  • jhoffner Avatar
Ad