Created
August 23, 2021 05:28
-
-
Save kermit-klein/94ae2b86289292e8cf9e64a977e6fd86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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