嗨,请帮助我,我的代码几乎完成,但它只触发最后一个按钮,即平方根选项。它应该看起来像一个计算器,打开一个带有文本框的弹出按钮,您可以在其中输入数字。我无法触发正确的操作。
<input type="button" value="Add" onclick="calculate('addition')">
<input type="button" value="Subtract" onclick="calculate('subtraction')">
<input type="button" value="Multiply" onclick="calculate('multiplication')">
<input type="button" value="Divide" onclick="calculate('division')">
<input type="button" value="Modulo" onclick="calculate('modulo')">
<input type="button" value="Pow" onclick="calculate('pow')">
<input type="button" value="Factorial" onclick="calculate('factorial')">
<input type="button" value="Sqrt" onclick="calculate('sqrt')">
<script>
function calculate(addition) {
var x= prompt("What is your first number?", "");
var y= prompt("What is your second number?", "");
var z= parseInt(x)+parseInt(y);
document.getElementById("z").value = z;
}
function calculate(subtraction) {
var x= prompt("What is your first number?", "");
var y= prompt("What is your second number?", "");
var z= parseInt(x)-parseInt(y);
document.getElementById("z").value = z;
}
function calculate(multiplication) {
var x= prompt("What is your first number?", "");
var y= prompt("What is your second number?", "");
var z= parseInt(x)*parseInt(y)
document.getElementById("z").value = z;
}
function calculate(division) {
var x= prompt("What is your first number?", "");
var y= prompt("What is your second number?", "");
var z= parseInt(x)/parseInt(y)
document.getElementById("z").value = z;
}
function calculate(modulo) {
var x= prompt("What is your first number?", "");
var y= prompt("What is your second number?", "");
var z= parseInt(x)%parseInt(y)
document.getElementById("z").value = z;
}
function calculate(pow) {
var x= prompt("What is your first number?", "");
var y= prompt("Raised to?", "");
var i=1
while (i<(parseInt(y)+1)) {
var z= parseInt(x)*parseInt(x)
i=parseInt(i)+1
}
document.getElementById("z").value = z;
}
function calculate(factorial) {
var x= prompt("What is your number?", "");
var i=1
var m=1
while (m<(parseInt(x))) {
i=parseInt(i)*((parseInt(m)+1))
m=parseInt(m)+1
}
document.getElementById("z").value = i;
}
function calculate(sqrt) {
var x= prompt("What is your number?", "");
i=1
while (i%parseInt(x)!=0) {
z=(parseInt(x)%i)
i=parseInt(i)+1
}
z=Math.sqrt(parseInt(x))
document.getElementById("z").value = z;
}
</script>
最佳答案
基本上,您正在重新声明calculate
。这不是必需的,但您需要评估该参数。根据内容进行计算。
function calculate(operator) {
var x, y, z;
if (operator === 'addition') {
x = parseInt(prompt("What is your first number?", ""), 10),
y = parseInt(prompt("What is your second number?", ""), 10),
z = x + y;
} else if (operator === 'subtraction') {
x = parseInt(prompt("What is your first number?", ""), 10),
y = parseInt(prompt("What is your second number?", ""), 10),
z = x - y;
}
document.getElementById("z").value = z;
}
对于多个比较,您可以使用 switch
运算符。
switch (operator) {
case 'addition':
z = x + y;
break; // to stop more testing
case 'subtraction':
z = x - y;
break;
// other cases
default:
z = 'unknown operator';
}
关于javascript - 简单的 Javascript 我不知道如何将函数放在 if-else 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40382625/