Javascript:如何在条件语句中使用运算符**
我想在字符串中找到一个运算符,如下面的Javascript:如何在条件语句中使用运算符**,javascript,conditional-statements,exponential,ternary,Javascript,Conditional Statements,Exponential,Ternary,我想在字符串中找到一个运算符,如下面的“3+7”,识别该运算符并进行求和。对于运算符+-*/,我有一个和平的代码,但对于**,我的代码返回布尔值 我想知道为什么会发生这种情况,以及如何使用具有条件的指数运算符 // To know if the string have a "+", "-", "*", "**" or "/" inside of him let foundCharacterFunc
“3+7”
,识别该运算符并进行求和。对于运算符+
-*
/
,我有一个和平的代码,但对于**
,我的代码返回布尔值
我想知道为什么会发生这种情况,以及如何使用具有条件的指数运算符
// To know if the string have a "+", "-", "*", "**" or "/" inside of him
let foundCharacterFunction = function(str, char) {
for(var i = 0; i < str.length; i++) {
if (str[i] == char) {
return char;
}
}
return false;
};
// With this we can use the specific character founded
let characterFoundAlready = foundCharacterFunction("3+7", "+");
// "characterFoundAlready" always is a string but work with all, less with exponential operator
console.log(typeof characterFoundAlready);
// To know what we going to do if we have one character or other
let enterTheCondition =
(characterFoundAlready === "+" ) ? console.log("We going to sum!") :
(characterFoundAlready === "-" ) ? console.log("We going to rest!") :
(characterFoundAlready === "*" ) ? console.log("We going to multiply!") :
(characterFoundAlready === "**" ) ? console.log("We going to power!!") :
(characterFoundAlready === "/" ) ? console.log("We going to divide!") :
console.log("That character it can't be procesed");
//知道字符串的内部是否有“+”、“-”、“*”、“***”或“/”
让foundCharacterFunction=函数(str,char){
对于(变量i=0;i
提前感谢您的时间和关注。我建议您阅读
Algo操作命令,而不是字符,并允许处理不同优先级的操作。你也应该这样
编辑:
下面是一些小示例,说明如何处理它:
const opertaionDescriptions = [
{ operation: '+', message: 'We going to sum!' },
{ operation: '-', message: 'We going to rest!' },
{ operation: '*', message: 'We going to multiply!' },
{ operation: '**', message: 'We going to power!' },
{ operation: '/', message: 'We going to divide!' },
];
let foundCharacterFunction = function (str) {
const found = str.match(/[\d\s]([+*/-]+)[\s\d]/i);
if (found && found[1]) {
return found[1];
}
return false;
};
// With this we can use the specific character founded
let characterFoundAlready = foundCharacterFunction("3+7");
// To know what we going to do if we have one character or other
console.log(
(opertaionDescriptions.find(({operation}) => operation === characterFoundAlready)
|| { message: "That character it can't be procesed"})
.message
);
// With this we can use the specific character founded
characterFoundAlready = foundCharacterFunction("3 ** 7");
// To know what we going to do if we have one character or other
console.log(
(opertaionDescriptions.find(({operation}) => operation === characterFoundAlready)
|| { message: "That character it can't be procesed"})
.message
);
当然,您仍然可以通过手动创建Regexp来定义精确的搜索操作,例如,smth
/[\d\s](char\u或string\u to\u search)[\s\d]/i
您希望一个字符变成两个字符的字符串。这是不可能的。if(str[i]==char)
一次检查一个字符。如果要检查两个字符的标记(如***
),则需要输入字符串。建议:考虑使用使用字符串不char,我在回答中添加了一个简单的例子,问题不是“中缀”与“RPN”。问题是OP的“解析”方法对于中缀表达式恰好有效,其中操作数是单个字符,如果操作数恰好是多个字符,则会失败。解决方案是更改他标识操作数的方式(即“以不同方式解析”)