我刚开始学习 javascript,我试图像计算器一样创建只是为了添加、减去、乘以和除以两个数字。但结果总是不确定的。你能告诉我问题出在哪里吗?
//Two functions
function output() {
var input1 = document.getElementById("nr1").value;
var input2 = document.getElementById("nr2").value;
var result = document.getElementById("result");
result.innerHTML = calculate(input1, input2).value;
}
function calculate(input1, input2) {
var add = document.getElementById("add");
var sub = document.getElementById("sub");
var mul = document.getElementById("mul");
var div = document.getElementById("div");
var rez
//How to know when we click one button?
if (add == true) {
rez = parseFloat(input1 + input2);
return rez;
} else if (sub == true) {
rez = parseFloat(input1 - input2);
return rez;
} else if (mul == true) {
rez = parseFloat(input1 * input2);
return rez;
} else if (div == true) {
rez = parseFloat(input1 / input2);
return rez;
} else {
return 0;
}
}
Helenr
慕慕森
相关分类