# Evaluating a string as a mathematical expression in JavaScript

We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function.

For example: If the equation is −

const str = '1+23+4+5-30';

Then the output should be 3

## Example

The code for this will be −

const str = '1+23+4+5-30';
const compute = (str = '') => {
let total = 0;
str = str.match(/[+\−]*(\.\d+|\d+(\.\d+)?)/g) || [];
while (str.length) {
total += parseFloat(str.shift());
};
console.log(compute(str));
3