2 回答

TA貢獻1719條經驗 獲得超6個贊
對我來說一切似乎都很好,只需根據給定的規范清理輸入方程,所有測試用例都將得到滿足。
對handleEqualClick()功能進行以下更改:
handleEqualsClick = () => {
const { input } = this.state;
//todo ? if the last char is oper, delete it
// console.log(input);
let eq = [];
for (let ch of [...input]) {
if (!eq.length) {
eq.push(ch);
} else {
if (ch === "-") {
eq.push("-");
} else if ("+-*/".includes(ch) && "+-*/".includes(eq[eq.length - 1])) {
while ("+-*/".includes(ch) && "+-*/".includes(eq[eq.length - 1])) {
eq.pop();
}
eq.push(ch);
} else {
eq.push(ch);
}
}
}
let sanitizedInput = eq.join("");
console.log(sanitizedInput);
let result = +eval(sanitizedInput).toFixed(7).toString();
console.log("result:", result);
if (result.length > 11) {
result = result.slice(0, 11);
}
this.setState((state) => ({
showInput: false,
output: result,
input: result //test14
}));
};

TA貢獻1946條經驗 獲得超3個贊
一些說明:我想要 1) 通過測試#13,2) 在顯示上看到正確的字符
功能上有錯誤fixInput
,我this.state.input
在需要更新版本時進行了測試,該版本是this.state.input + value
.
function fixInput(inputStr, currentOp) {
? ? const lastTwoOp = /([+\-*/]{2})$/;
? ? const lastThreeOp = /([+\-*/]{3})$/;
? ? const allowTwo = /(\*|\/)-/;
? ? const toTest = inputStr + currentOp; //<=?
? ? if (lastTwoOp.test(toTest) && !allowTwo.test(toTest)) {
? ? ? ? inputStr = inputStr.slice(0, -1) + currentOp;
? ? } else if (lastThreeOp.test(toTest)) {
? ? ? ? inputStr = inputStr.slice(0, -2) + currentOp;
? ? } else {
? ? ? ? inputStr += currentOp;
? ? }
? ? return inputStr;
}
添加回答
舉報