javascript - 如何 .search() 多个字符串并至少有 1 个字符串才能起作用?

标签 javascript html

我正在为我的最终高中数学项目编写一个计算器,但我似乎找不到如何 .search() 多个字符串,如果该元素至少有一个,那么它就可以工作。

    function pie(val) {
  var pi = document.getElementById("display").value.search("/" | "*" | "-" | "+");
  if (pi != -1) {
    document.getElementById("display").value += val;
  }
  else {
    document.getElementById("display").value = "Please add an operator";
}
}

我希望 Pi 仅在有运算符时才执行,因为如果没有运算符,它只会将 PI 添加到屏幕上的任何数字。

// Clear Function
function c(val) {
  document.getElementById("display").value = val;
}
// Insert Number Function
function insert(val) {
  document.getElementById("display").value += val;
}
// Equal Function
function equal(val) {
  try {
    c(eval(document.getElementById("display").value))
  }
catch(equal) {
    document.getElementById("display").value = "error";
    }
}
// Root Function
function root(val) {
  var vroot = document.getElementById("display").value;
  var rooted = Math.sqrt(document.getElementById("display").value);
  var r = document.getElementById("display").value = rooted;

if (vroot< 0) {
    document.getElementById("display").value= "no";
  }
}
// Pie Fonction
function pie(val) {
  var pi = document.getElementById("display").value.search("/" | "*" | "-" | "+");
  if (pi != -1) {
    document.getElementById("display").value += val;
  }
  else {
    document.getElementById("display").value = "Please add an operator";
}
}
// Power function : fix so power of any number
function power(val) {
  var base = document.getElementById("display").value;
  document.getElementById("display").value = base * base;
}
// Log Function
function log(val) {
  var vlog = document.getElementById("display").value;
  var vloged = Math.log(document.getElementById("display").value);
  var v = document.getElementById("display").value = vloged;
  if (vlog < 0) {
    document.getElementById("display").value= "no";
  }
}

function cos(val) {
  var vroot = document.getElementById("display").value;
  var rooted = Math.sqrt(document.getElementById("display").value);
  var r = document.getElementById("display").value = "√" + vroot + "=" + rooted;
}

function sin(val) {
  var vroot = document.getElementById("display").value;
  var rooted = Math.sqrt(document.getElementById("display").value);
  var r = document.getElementById("display").value = "√" + vroot + "=" + rooted;
}

function tan(val) {
  var vroot = document.getElementById("display").value;
  var rooted = Math.sqrt(document.getElementById("display").value);
  var r = document.getElementById("display").value = "√" + vroot + "=" + rooted;
}
.calculator {
text-align: center;
}
.display input {
  margin-left: 5px;
  font-size: 50px;
  width: 600px;
  height: 100px;
  border-radius: 10px;
  background-color: rgb(217, 217, 217);
  border-color: rgb(230,230,230, .5);
  color: rgb(153, 153, 153);
  margin-top: 25px;
}

.display input:focus {
  outline:0;
}

.button {
  background-color: rgb(230, 230, 230, .5);
  font-size: 50px;
  height: 100px;
  width: 100px;
  text-align: center;
  margin:3px;
  color: rgb(153, 153, 153);
  border-radius: 10px;
  border-color: rgb(230,230,230, .5);
}
.button:hover {
background-color: white;
}

.button:focus {
  outline:0;
}

body {
  background-color: rgb(204, 204, 204);
}
<!DOCTYPE html>
<html>
<head>
<title> Calculator </title>
</head>
<body>


<div class="calculator">
<div class="display">
  <input type="text" id="display">
</div>
<div class="main">
    <br>
    <center>
<table>
  <tr>
  <td><input type="button" class="button" value="C" onclick='c("")'></td>
  <td><input type="button" class="button" value="π" onclick='pie(Math.PI)'></td>
  <td><input type="button" class="button" value="√" onclick='root()'></td>
  <td><input type="button" class="button" value="^" onclick='power()'></td>
  <td><input type="button" class="button" value="*" onclick='insert("*")'></td>
</tr>

<tr>
  <td><input type="button" class="button" value="log" onclick='log()'></td>
  <td><input type="button" class="button" value="7" onclick='insert(7)'></td>
  <td><input type="button" class="button" value="8" onclick='insert(8)'></td>
  <td><input type="button" class="button" value="9" onclick='insert(9)'></td>
  <td><input type="button" class="button" value="/" onclick='insert("/")'></td>
</tr>

<tr>
  <td><input type="button" class="button" value="cos" onclick='cos()'></td>
  <td><input type="button" class="button" value="4" onclick='insert(4)'></td>
  <td><input type="button" class="button" value="5" onclick='insert(5)'></td>
  <td><input type="button" class="button" value="6" onclick='insert(6)'></td>
  <td><input type="button" class="button" value="+" onclick='insert("+")'></td>
</tr>

<tr>
  <td><input type="button" class="button" value="sin" onclick='sin()'></td>
  <td><input type="button" class="button" value="1" onclick='insert(1)'></td>
  <td><input type="button" class="button" value="2" onclick='insert(2)'></td>
  <td><input type="button" class="button" value="3" onclick='insert(3)'></td>
  <td><input type="button" class="button" value="-" onclick='insert("-")'></td>
</tr>

<tr>
  <td><input type="button" class="button" value="tan" onclick='tan()'></td>
  <td><input type="button" class="button" value="?" onclick='insert("")'></td>
  <td><input type="button" class="button" value="0" onclick='insert(0)'></td>
  <td><input type="button" class="button" value="." onclick='insert(".")'></td>
  <td><input type="button" class="button" value="=" onclick='equal()'></td>
</tr>
</table>
</center>
</div>


</div>
</body>

</html>

最佳答案

尝试重写这一行

var pi = document.getElementById("display").value.search("/" | "*" | "-" | "+");

对此:

 var pi = ["/", "*", "-", "+"].indexOf(document.getElementById("display").value);

关于javascript - 如何 .search() 多个字符串并至少有 1 个字符串才能起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181299/

相关文章:

javascript - 将计算属性与其他计算属性组合

javascript - 谷歌图表尾随零

javascript - 使固定的顶部元素随页面滚动(一旦内容到达它)

html - 在 950 像素宽的 div 中居中 1250 像素宽的图像 - CSS 问题

html - 基金会 5 : Columns order issue in responsive layout (demonstration included)

html - 在 css 中将宽度不均匀的元素均匀分布在父元素的整个宽度上

javascript - 将 Razor DropDownList 保持在一行上

javascript - 在 Bootstrap 中隐藏和显示按钮

javascript - 定位给定标签的所有最后(但不是最后)元素

javascript - bootstrap aside 在小型设备上隐藏分页元素