This is my attempt at it
const handleEqual = () => {
const plusRegex = /\d(([/*-]+)?)?\+\d/g;
const mulRegex = /\d(([/*+-]+)?)?\*\d/g;
const divRegex = /\d(([/*+-]+)?)?\/\d/g;
if (calc.input.match(plusRegex)) {
setCalc(calc.input.replace(plusRegex, ""));
} else if (calc.input.match(mulRegex)) {
setCalc(calc.input.replace(mulRegex, ""));
} else if (calc.input.match(divRegex)) {
setCalc(calc.input.replace(divRegex, ""));
} else {
setCalc({ input: math.evaluate(calc.input).toString() });
}
setCalc({ input: math.evaluate(calc.input).toString() });
};
Here is a link to my code:
calculator by JudgeFudge19 using jquery, mathjs, react, react-dom, react-scripts, styled-components
If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.
You need to implement some code to handle the negative numbers
there’s also an error on this
12. I should be able to perform any operation (+, -, *, /) on numbers containing decimal points
and this link might help u out
In this article we have covered the fundamental information you need to know about numbers in JavaScript, for now. You'll see numbers used again and again, all the way through your JavaScript learning, so it's a good idea to get this out of the way...
1 Like
Hey! Thank you
Updated it to
const handleEqual = () => {
const plusRegex = /(([/*-]+)?)?\+/g;
const mulRegex = /(([/*+-]+)?)?\*/g;
const divRegex = /(([/*+-]+)?)?\//g;
const subRegex = /(([/*+-]+)?)?(([/*+-])?)?-/g;
if (calc.input.match(plusRegex)) {
setCalc(calc.input.replace(plusRegex, "+"));
} else if (calc.input.match(mulRegex)) {
setCalc(calc.input.replace(mulRegex, "*"));
} else if (calc.input.match(divRegex)) {
setCalc(calc.input.replace(divRegex, "/"));
} else if(calc.input.match(subRegex)){
if(calc.input.match(/(([/*+-]+)?)?\+-/g)){
setCalc(calc.input.replace(/(([/*+-]+)?)?\+-/g, '+-'))
} else if(calc.input.match(/(([/*+-]+)?)?\*-/g)){
setCalc(calc.input.replace(/(([/*+-]+)?)?\*-/g, '*-'))
} else if(calc.input.match(/(([/*+-]+)?)?\/-/g)){
setCalc(calc.input.replace(/(([/*+-]+)?)?\/-/g, '/-'))
} else if(calc.input.match(/(([/*+-]+)?)?--/g)){
setCalc(calc.input.replace(/(([/*+-]+)?)?--/g, '--'))
}
}
setCalc({ input: math.evaluate(calc.input).toString() });
};
But i still can’t pass test 13
Ah yes, i asked for help in test 12 in another topic because I’m using mathjs to evaluate calculations and i need help tweaking the logic i used to pass test 11 to allow test 12 to pass
Finally solved this. Remembered that strings are immutable so, in every place i used the replace function i reassigned the string value i.e
const handleEqual = () => {
const plusRegex = /(([/*-]+)?)?\+/g;
const mulRegex = /(([/*+-]+)?)?\*/g;
const divRegex = /(([/*+-]+)?)?\//g;
const subRegex = /(([/*+-]+)?)?(([/*+-])?)?-/g;
if (calc.input.match(plusRegex)) {
setCalc((calc.input = calc.input.replace(plusRegex, "+")));
} else if (calc.input.match(mulRegex)) {
setCalc((calc.input = calc.input.replace(mulRegex, "*")));
} else if (calc.input.match(divRegex)) {
setCalc((calc.input = calc.input.replace(divRegex, "/")));
} else if (calc.input.match(subRegex)) {
if (calc.input.match(/(([/*+-]+)?)?\+-/g)) {
setCalc((calc.input = calc.input.replace(/(([/*+-]+)?)?\+-/g, "+-")));
} else if (calc.input.match(/(([/*+-]+)?)?\*-/g)) {
setCalc((calc.input = calc.input.replace(/(([/*+-]+)?)?\*-/g, "*-")));
} else if (calc.input.match(/(([/*+-]+)?)?\/-/g)) {
setCalc((calc.input = calc.input.replace(/(([/*+-]+)?)?\/-/g, "/-")));
} else if (calc.input.match(/(([/*+-]+)?)?--/g)) {
setCalc((calc.input = calc.input.replace(/(([/*+-]+)?)?--/g, "--")));
}
}
setCalc({ input: math.evaluate(calc.input).toString() });
};
Thank you @KittyKora
If you have any insight on how i can handle my other error pls help
1 Like