我试图使用 this.handleFormula 函数在 div 中呈现字符串的最后一个值。我将此函数分配给 this.state 中的公式并将 this.state.formula 链接到 div。我试图 console.log 返回值,但仍然没有渲染,你可能是什么问题?
这是我的代码笔: https ://codepen.io/tonytony92/pen/bGdmbzx ?editors=1111
this.state={
display:"0",
formula:this.handleFormula,
buttonState:[{key: 'AC',
col:'red',
key1:"AC"
},///and a bunch of other objects in array/// ] }
handleFormula(){
let str=this.state.display
console.log(str)
let newArr=str.match(/["/"+"x"-]/gi)
let newStr=""
if (Boolean(newArr)==false){//If no special characters are found in string ,return current display//
console.log(str)
return str
}
else{
for(let i=newArr.length-1;i>-1;i--){// if last value is a number concatenate it to a variable///
if(parseFloat(newArr[i])!==NaN){
newStr+=newArr[i]
}
else if(i===newArr.length-1&& parseFloat(newArr[i])===NaN){ // if last value is a specialChar return it///
return newArr[i]
}
else{ /// return numbers concatenated in variable///
return newStr
}
}
}
}
森林海
慕娘9325324
相关分类