Skip to content

Instantly share code, notes, and snippets.

@kermit-klein
Created August 23, 2021 05:28
Show Gist options
  • Save kermit-klein/94ae2b86289292e8cf9e64a977e6fd86 to your computer and use it in GitHub Desktop.
Save kermit-klein/94ae2b86289292e8cf9e64a977e6fd86 to your computer and use it in GitHub Desktop.
const calcString = (str) => {
let seperatedStr = str.split(" ");
if (
!seperatedStr.every((e) => /^-?([1-9][0-9]*|([0]))$|^[\+\*\/\-]$/.test(e)) // Wanted to check input validity, can be removed
) {
throw SyntaxError("Invalid Format");
}
let definedOps = [
{ "*": (x, y) => x * y, "/": (x, y) => x / y },
{ "+": (x, y) => parseFloat(x) + parseFloat(y), "-": (x, y) => x - y },
];
let nyCalc = [];
let opType;
for (let i = 0; i < definedOps.length; i++) {
for (let j = 0; j < seperatedStr.length; j++) {
if (definedOps[i][seperatedStr[j]]) {
opType = definedOps[i][seperatedStr[j]];
} else if (opType) {
nyCalc[nyCalc.length - 1] = opType(
nyCalc[nyCalc.length - 1],
seperatedStr[j]
);
opType = null;
} else {
nyCalc.push(seperatedStr[j]);
}
}
seperatedStr = nyCalc;
nyCalc = [];
}
return seperatedStr[0];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment