Calculating Mathematical Expressions

Published by bangyen in

Create a function that takes a mathematical function as a String, an Array of numbers on which the mathematical function is to be called, and returns the result as an Array of Strings.

Examples

math_func("f(y)=y+1", [1, 2]) ➞ ["f(1)=2", "f(2)=3"]

math_func("f(y)=y^2", [1, 2, 3]) ➞ ["f(1)=1", "f(2)=4", "f(3)=9"]

math_func("f(y)=yx3", [1, 2, 3]) ➞ ["f(1)=3", "f(2)=6", "f(3)=9"]

Notes

  • Each array will only contain positive numbers.
  • The letter x denotes multiplication.
Watch a quick demo on how Edabit works.