我为最后的高中数学项目编写了一个计算器,我似乎无法找到如何使用.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;
}
&#13;
.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);
}
&#13;
<!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>
&#13;
答案 0 :(得分:1)
尝试重写此行
var pi = document.getElementById("display").value.search("/" | "*" | "-" | "+");
对此:
var pi = ["/", "*", "-", "+"].indexOf(document.getElementById("display").value);
答案 1 :(得分:0)
你应该使用Array.some。
如果您的值与Array中的任何一个值匹配,则返回true,否则返回false。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
答案 2 :(得分:0)
搜索采用正则表达式。如果您正在寻找其中任何一个,请使用/ [/ * + - ] /作为搜索参数。 - 麦克麦考恩
他对此发表了评论,所以我不知道该怎么做才能解决这个问题。