Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions apps/javascript-calculator/client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably remove the spaces for here and above.

Copy link
Author

@CallmeHongmaybe CallmeHongmaybe Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.
Also can you help with the tests ? Because I'm currently on a stretch.

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:
Expand All @@ -70,7 +80,7 @@ class Calculator extends React.Component {
.replace(/-/g, '-')
.replace(/(x|\/|\+)-/, '$1-')
.replace(/^-/, '-') +
'=' +
'=' +
answer,
prevVal: answer,
evaluated: true
Expand Down