我似乎在试图弄清楚如何使它正常工作时遇到问题-尝试传递字符串并检测字符串是否具有特定的运算符时,我继续遇到此错误,如果这样,则使用switch语句运行操作-有没有更好的方法让我使用switch语句在字符串中搜索运算符?
我的打字稿功能:
function getAns(){
var FinalSum = (<HTMLInputElement>document.getElementById('input')).value
switch(FinalSum){
case FinalSum.indexOf("%"): //Type 'number' is not comparable to type 'string'.
try{
getPercent();
}catch(Error){
InvalidEntry()
}
break;
case FinalSum.indexOf("^"): //Type 'number' is not comparable to type 'string'.
try{
getPercent();
}catch(Error){
InvalidEntry()
}
break;
default:
try{
var inputSum = (<HTMLInputElement>document.getElementById('input'))
var ans = eval(inputSum.value);
(<HTMLInputElement>document.getElementById('answer')).value = ans;
(<HTMLInputElement>document.getElementById('input')).placeholder = (<HTMLInputElement>document.getElementById('input')).value;
(<HTMLInputElement>document.getElementById('input')).value = "";
}catch(Error){
InvalidEntry();
}
break;
}
}
[我似乎在试图弄清楚如何使它正常工作时遇到问题-尝试传递字符串并检测字符串是否具有特定的运算符,如果是这样的话,我仍然会遇到此错误,如果是这样...
switch (FinalSum) {
表示将每种情况与finalSum
进行比较。如果大小写表达式为===
至finalSum
,则将满足大小写。