Javascript React应用程序在我运行测试时冻结浏览器页面
我是React新手,目前正在使用React框架开发具有基本功能的计算器应用程序。这是freecodecamp前端库项目的一部分。我的代码可以正常工作,因为我可以进行所有计算,但每次我尝试运行测试时,网页都会冻结。这是我的密码Javascript React应用程序在我运行测试时冻结浏览器页面,javascript,reactjs,calculator,Javascript,Reactjs,Calculator,我是React新手,目前正在使用React框架开发具有基本功能的计算器应用程序。这是freecodecamp前端库项目的一部分。我的代码可以正常工作,因为我可以进行所有计算,但每次我尝试运行测试时,网页都会冻结。这是我的密码 class Calculator extends React.Component{ constructor(props){ super(props) this.state = { display: "0",
class Calculator extends React.Component{
constructor(props){
super(props)
this.state = {
display: "0",
clickedNumber: false,
clickedOperator: false,
clickedDecimal: false,
}
this.handleClear = this.handleClear.bind(this)
this.inputNums = this.inputNums.bind(this)
this.doEquals = this.doEquals.bind(this)
this.inputOperator = this.inputOperator.bind(this)
this.inputDecimal = this.inputDecimal.bind(this)
}
handleClear(){
this.setState({display: "0",
clickedNumber: false,
clickedOperator: false,
clickedDecimal: false})
}
inputNums(Buttons){
if(!this.state.clickedNumber && Buttons.target.innerText !== "0")
{this.setState({display: Buttons.target.innerText,
clickedNumber: true,
clickedDecimal: false})}
else if(this.state.clickedNumber){
this.setState({display: this.state.display + Buttons.target.innerText,
})
}
}
inputOperator(operator){
if(!this.state.clickedOperator){
this.setState({
display: this.state.display + operator.target.innerText,
clickedDecimal: false })
}
}
inputDecimal(decimal){
if(!this.state.clickedDecimal){
this.setState({
display: this.state.display + decimal.target.innerText,
clickedDecimal: true,
clickedNumber: true,
clickedOperator:false
})
}
}
doEquals(){
function makingRequestedOperation(arr, operIndex, oper){
let num1 = [];
let num2=[];
let info = {
}
let result;
for(var j = parseInt(operIndex)-1; j>-1; j--){
if(!isNaN(arr[j]) || arr[j] === "."){
num1.push(arr[j]);
info.firstNumIndex = j;
}else if(isNaN(arr[j]) && arr[j] !== "."){
break;
}
}
num1.reverse();
for(var i = parseInt(operIndex)+1; i<arr.length; i++){
if(!isNaN(arr[i]) || arr[i] === "."){
num2.push(arr[i]);
info.lastNumIndex = i;
}else if(isNaN(arr[i]) && arr[i] !== "."){
break;
}
}
const deleteCount = num1.concat(oper,num2);
let operator1 = num1.join("");
let operator2 = num2.join("");
if(oper==="+"){
result = parseFloat(operator1) + parseFloat(operator2);
}else if(oper ==="-"){
result = parseFloat(operator1) - parseFloat(operator2)
}else if(oper ==="*"){
result = parseFloat(operator1) * parseFloat(operator2);
}else if(oper ==="/"){
result = parseFloat(operator1) / parseFloat(operator2);
}
info.result = result;
arr.splice(info.firstNumIndex,deleteCount.length,info.result);
return arr;
}
let arr = this.state.display.split("");
while(arr.length>1){
let sumIndex = arr.indexOf("+");
let divIndex = arr.indexOf("/");
let multIndex = arr.indexOf("*");
let subIndex = arr.indexOf("-")
if(divIndex > 0 && multIndex >0){
if(divIndex < multIndex){
arr = makingRequestedOperation(arr, divIndex, "/");
}else{
arr = makingRequestedOperation(arr, multIndex, "*");
}
}else{
if(divIndex > 0){
arr = makingRequestedOperation(arr, divIndex, "/");
}else if(multIndex >0){
arr = makingRequestedOperation(arr, multIndex, "*");
}else if(sumIndex >0 && subIndex > 0){
if(sumIndex < subIndex){
arr = makingRequestedOperation(arr, sumIndex, "+");
}else{
arr = makingRequestedOperation(arr, subIndex, "-");
}
}else if(subIndex >0){
arr = makingRequestedOperation(arr, subIndex, "-");
}else{
arr = makingRequestedOperation(arr, sumIndex, "+");
}
}
}
arr = Math.round(arr*100000)/100000;
this.setState({
display: arr.toString()
})
}
render(){
const buttonsData =
[{id:"zero",value: "0"},
{id:"one", value: "1"},{id:"two",value: '2'},
{id:"three",value: "3"},{id:"four",value: "4"},{id:"five",value: "5"},
{id:"six",value: "6"},{id:"seven",value: "7"},{id:"eight",value: "8"},
{id:"nine",value: "9"}];
const operators = [{
id:"add", value: "+"},{ id:"subtract", value: "-"
},{id:"multiply", value: "*"},{id:"divide", value: "/"}]
return(
<div className = "App">
<Display
display = {this.state.display}
/>
{buttonsData.map((data) =>
(<Buttons
id = {data.id}
name = {data.value}
key = {data.id}
onClick = {this.inputNums}
/>
)) }
<Buttons
id = "decimal"
onClick = {this.inputDecimal}
name = "."
/>
{operators.map((operator) =>(<Buttons
id = {operator.id}
name = {operator.value}
key = {operator.id}
onClick = {this.inputOperator}
/>))}
<Buttons
id= "equals"
name = "="
onClick = {this.doEquals}
/>
<Buttons
id = "clear"
onClick = {this.handleClear}
name = "Clear"
/>
</div>
)
}
}
类计算器扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
显示:“0”,
clickedNumber:false,
单击运算符:false,
clickedDecimal:false,
}
this.handleClear=this.handleClear.bind(this)
this.inputNums=this.inputNums.bind(this)
this.doEquals=this.doEquals.bind(this)
this.inputOperator=this.inputOperator.bind(this)
this.inputDecimal=this.inputDecimal.bind(this)
}
handleClear(){
this.setState({display:“0”,
clickedNumber:false,
单击运算符:false,
clickedDecimal:false})
}
输入(按钮){
如果(!this.state.clickedNumber&&Buttons.target.innerText!=“0”)
{this.setState({display:Buttons.target.innerText,
clickedNumber:true,
clickedDecimal:false}
else if(此.state.clickedNumber){
this.setState({display:this.state.display+Buttons.target.innerText,
})
}
}
输入操作员(操作员){
如果(!this.state.clickedOperator){
这是我的国家({
显示:this.state.display+operator.target.innerText,
clickedDecimal:false})
}
}
输入十进制(十进制){
如果(!this.state.clickedDecimal){
这是我的国家({
显示:this.state.display+decimal.target.innerText,
clickedDecimal:对,
clickedNumber:true,
单击运算符:false
})
}
}
多奎尔斯(){
函数生成请求操作(arr、operIndex、oper){
设num1=[];
设num2=[];
让信息={
}
让结果;
对于(var j=parseInt(operIndex)-1;j>-1;j--){
如果(!isNaN(arr[j])| | arr[j]==“){
num1.推力(arr[j]);
info.firstNumIndex=j;
}else if(isNaN(arr[j])&&arr[j]!=“){
打破
}
}
num1.reverse();
对于(var i=parseInt(operIndex)+1;i1){
设sumIndex=arr.indexOf(“+”);
设DIVIDEX=arr.indexOf(“/”);
设multIndex=arr.indexOf(“*”);
设子索引=arr.indexOf(“-”)
如果(divIndex>0&&multIndex>0){
if(指数<多重指数){
arr=进行请求操作(arr,divIndex,“/”;
}否则{
arr=进行请求操作(arr,多重索引,“*”);
}
}否则{
如果(divIndex>0){
arr=进行请求操作(arr,divIndex,“/”;
}否则如果(多指标>0){
arr=进行请求操作(arr,多重索引,“*”);
}else if(sumIndex>0&&subIndex>0){
if(sumIndex<子索引){
arr=进行请求操作(arr,SUMDINDEX,“+”;
}否则{
arr=进行请求操作(arr,子索引“-”);
}
}否则如果(子索引>0){
arr=进行请求操作(arr,子索引“-”);
}否则{
arr=进行请求操作(arr,SUMDINDEX,“+”;
}
}
}
arr=数学整数(arr*100000)/100000;
这是我的国家({
显示:arr.toString()
})
}
render(){
常量按钮数据=
[{id:“零”,值:“0”},
{id:“1”,值:“1”},{id:“2”,值:'2'},
{id:“三”,value:“3”},{id:“四”,value:“4”},{id:“五”,value:“5”},
{id:“六”,value:“6”},{id:“七”,value:“7”},{id:“八”,value:“8”},
{id:“九”,value:“9”}];
常量运算符=[{
id:“添加”,值:“+”},{id:“减去”,值:“-”
},{id:“乘”,值:“*”},{id:“除”,值:“/”}]
返回(
{buttonsData.map((数据)=>
(
)) }
{operators.map((operator)=>())}
)
}
}
我认为问题可能来自doEquals函数,但我无法解决它。我如何解决这个问题