[英]FreeCodeCamp Javascript calculator test suite | I don't know why I can't pass this test
So I'm doing that FCC javascript calculator test, and I'm just stuck.所以我正在做 FCC javascript 计算器测试,我只是卡住了。
my code seems to be working but test suite says I'm not getting half of it so I just wanted to ask if you guys can point me to a write direction.我的代码似乎在工作,但测试套件说我没有得到一半,所以我只想问你们是否可以指出我的写作方向。
Also I'm keep getting this msg:我也不断收到这个消息:
Uncaught SyntaxError: Invalid left-hand side in assignment.
But I don't know what it's referring to.但我不知道它指的是什么。
const equalStyle = {
height: 120,
backgroundColor: "#995ba4",
bottom:7,
position:"absolute"
};
const clearStyle = {backgroundColor: "#b83146"};
const equationStyle = {backgroundColor: "#b2b2b2"};
const equation = /[/*+-]/;
const confirmEquation = /[/*+-]$/;
const secondConfirm =/\d[x/+‑*]‑$/;
const negative = /[-]/;
const dotTester = /[.]/;
const prevTester = /^[/*-+]/;
class Main extends React.Component{
constructor(props){
super(props);
this.state={
totalVal:"",
testVal:"0",
displayVal:"0",
prevVal:""
}
this.clickedVal = this.clickedVal.bind(this);
this.calDisplay = this.calDisplay.bind(this);
}
calDisplay(val){
if(this.state.displayVal == "0"){
if(this.state.totalVal == ""){
this.setState({
totalVal: val
})
}
}
else{
this.setState({totalVal:this.state.totalVal + val})
}
}
clickedVal(e){
let clickVal = e.target.value;
if (clickVal=="x"){clickVal = clickVal.replace("x", "*")};
if(this.state.totalVal.includes("=")){
this.setState({
totalVal:"",
testVal:"0",
displayVal:"0"
})
}
setTimeout(()=>{
if(this.state.testVal == "0"&&!isNaN(parseInt(clickVal))){
this.setState({ displayVal:clickVal, testVal:clickVal}, this.calDisplay(clickVal))
}
else if (!isNaN(parseInt(clickVal))){
this.setState({ displayVal: this.state.displayVal + clickVal,testVal:this.state.displayVal + clickVal},this.calDisplay(clickVal))
}
/*equation stuff*/
else if(equation.test(clickVal)){
if(confirmEquation.test(this.state.totalVal)){
if(negative.test(clickVal)){
this.calDisplay(clickVal)
this.setState({displayVal: clickVal, testVal:"0"})
}
else if(secondConfirm.test(this.state.totalVal)){
this.setState({
displayVal: clickVal, testVal:"0", totalVal: this.state.totalVal.slice(0, this.state.totalVal.length-2)
});
setTimeout(()=>this.calDisplay(clickVal))
}
else{
this.setState({
displayVal: clickVal, testVal:"0", totalVal: this.state.totalVal.slice(0, this.state.totalVal.length-1)
});
setTimeout(()=>this.calDisplay(clickVal))
}
}
else if (typeof(this.state.prevVal) == "number" && this.state.displayVal =="0"){
this.setState({displayVal:clickVal, testVal:"0"})
this.calDisplay(this.state.prevVal+clickVal)
}
else{
this.setState({displayVal:clickVal, testVal:"0"})
this.calDisplay(clickVal);
}
}
else if(clickVal == "."){
if(confirmEquation.test(this.state.totalVal)){
this.calDisplay("0.")
this.setState({ displayVal: "0.", testVal: "0"})
}
else if (dotTester.test(this.state.displayVal)){
this.setState({displayVal: this.state.displayVal ,testVal:this.state.displayVal})
}
else{
this.calDisplay(clickVal);
this.setState({displayVal: this.state.displayVal + clickVal,testVal:this.state.displayVal + clickVal})
}
}
else if (clickVal == "="){
let result = Math.round(1000000000000 * eval(this.state.totalVal)) / 1000000000000;
this.setState({
displayVal: result, testVal:"0", prevVal:result
})
this.calDisplay("="+result)
}
else if (clickVal == "AC"){
this.setState({
totalVal:"",
testVal:"0",
displayVal:"0",
prevVal:""
})
}
})
}
render(){
return(
<div className = "calculatorBody" >
<OutputDisplay formula = {this.state.totalVal}/>
<InputDisplay currentValue = {this.state.displayVal} />
<Buttons onClick = {this.clickedVal}/>
</div>
)
}
}
https://codepen.io/arthur-lee945/pen/NWaebeV?editors=0111 https://codepen.io/arthur-lee945/pen/NWaebeV?editors=0111
This is my codepen for it.这是我的代码笔。
The error occurs when 'this.state.totalVal' is "5-2=3/2" at below codes(117 line).当以下代码(117 行)的“this.state.totalVal”为“5-2=3/2”时,会发生错误。
let result = Math.round(1000000000000 * eval(this.state.totalVal)) / 1000000000000;
And you can check values of this variable by setting a breakpoint at 117 line in the source tab of your browser devtools.您可以通过在浏览器开发工具的源选项卡中的 117 行设置断点来检查此变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.