Tell us what’s happening:
So I am really stuck on the FCC calculator. I am passing all tests except:
" 13. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign."
I am using the MathJS library. After a lot of frustration I just wanted to get a working version going before coming back and refining the project. In the end I want to implement a RPN algorithm to solve the expressions. But one step at a time I guess.
This code is probably a huge mess to most of you guys so I ll apologize in advance. In an attempt to make it somewhat modular I might have overengineered it so much its unusable. Or it might be just plain bad code, could be
My problem is that I am stuck on sanitizing the input to only use the last operator.
5 * - + 5 will return -25 instead of 10. Of course -+5 is technically -5, so the result should be -25 as expected, but obiously it does not solve the issue.
I need a way to check the final string for the last operator ( in this case +) and delete all operators preceding that operator until I hit a number again. My brain has melted. I am pretty sure I could solve this using regex and/or a reverse array traversal but I am somewhat lost.
Send help
Link to repo: Click
Link to calculator: Click
Your code so far
const handleClick = event => {
let obj = parseEvent(event);
let inputVal = `${obj.text}`;
let prev = `${displayFormulaText[displayFormulaText.length - 1]}`;
/**
* We need to divide into three groups: Functions, Operators, Numbers
*/
//Handle functions
if (obj.isfunction) {
switch (true) {
case obj.text === 'AC':
clear();
break;
case obj.text === '=':
calculate();
break;
case obj.text === 'C':
clearOne();
break;
case obj.text === '%':
break;
default:
break;
}
} else {
//Type in the result display, only add it to formula once an operator shows up
if (!obj.isoperator) {
// First entry should remove the zero
if (firstEntry) {
if (displayResultText.match(/0/)) {
setDisplayResultText(inputVal);
setDisplayFormulaText(inputVal);
setFirstEntry(false);
return;
}
}
if (inputVal === '.' && displayResultText.includes('.')) return;
if (inputVal === '0' && prev === '0') return;
setDisplayFormulaText(displayFormulaText.concat(inputVal));
setDisplayResultText(displayResultText.concat(inputVal));
} else {
// is operator
if (firstEntry) {
if (inputVal.match(/[///*+]/) && displayResultText.match(/0/)) {
setFirstEntry(false);
return;
}
}
// If the input is +-*/ and the previous is +-*/
if (inputVal.match(/[///*+]/) && prev.match(/[///*+]/)) {
console.log(inputVal);
return;
}
setDisplayFormulaText(displayFormulaText.concat(inputVal));
setDisplayResultText(inputVal);
}
}
};
const clear = () => {
setDisplayFormulaText('');
setDisplayResultText('0');
setFirstEntry(true);
};
const clearOne = () => {
setDisplayFormulaText(
displayFormulaText.slice(0, displayFormulaText.length - 1)
);
};
const calculate = () => {
setDisplayResultText(`${evaluate(displayFormulaText)}`);
setDisplayFormulaText(`${evaluate(displayFormulaText)}`);
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
.
Challenge: Build a JavaScript Calculator
Link to the challenge: