diff --git a/apps/javascript-calculator/client/index.jsx b/apps/javascript-calculator/client/index.jsx index 8dc95a13..84bb5576 100644 --- a/apps/javascript-calculator/client/index.jsx +++ b/apps/javascript-calculator/client/index.jsx @@ -54,14 +54,24 @@ class Calculator extends React.Component { handleEvaluate() { if (!this.state.currentVal.includes('Limit')) { let expression = this.state.formula; + let zeroOperationPattern = new RegExp(/[+-]+0(?!\.)/g) + + expression = expression + .replace(/x/g, '*') + .replace(/‑/g, '-') + while (endsWithOperator.test(expression)) { expression = expression.slice(0, -1); } - expression = expression - .replace(/x/g, '*') - .replace(/-/g, '-') - .replace('--', '-'); - let answer = Math.round(1000000000000 * eval(expression)) / 1000000000000; + + let zeroPatternMatches = expression.match(zeroOperationPattern) + + if (zeroPatternMatches) { + zeroPatternMatches.forEach(match => expression = expression.replace(match, '')) + } + + let answer = eval(expression) + this.setState({ currentVal: answer.toString(), formula: @@ -70,7 +80,7 @@ class Calculator extends React.Component { .replace(/-/g, '-') .replace(/(x|\/|\+)-/, '$1-') .replace(/^-/, '-') + - '=' + + '=' + answer, prevVal: answer, evaluated: true