我的目标是创建一个带有长度单位的真正的数学表达式。我唯一遇到麻烦的部分是 Javascript 的 if else 部分。我应该更改什么,以便长度单位的 if else 语句起作用?这是我到目前为止所做的:
var operators = ['+', '-'];
var e = ['km', 'm'];
function F1() {
num1 = document.getElementById("num1");
num2 = document.getElementById("num2");
rnum1 = Math.floor((Math.random() * 10) + 1);
rnum2 = Math.floor((Math.random() * 10) + 1);
num1.innerHTML = rnum1;
num2.innerHTML = rnum2;
oper = document.getElementById("operator");
op = operators[Math.floor(Math.random() * 2)];
oper.innerHTML = op;
eht = document.getElementById("e1");
eht2 = document.getElementById("e2");
eh = e[Math.floor(Math.random() * e.length)];
eh2 = e[Math.floor(Math.random() * e.length)];
eht.innerHTML = eh;
eht2.innerHTML = eh2;
answer = document.getElementById("answer");
if (eh = 'km') {
if (eh2 = 'm') {
answer.innerHTML = eval(rnum1 * 1000 + op + rnum2);
} else {
if (eh = 'm') {
if (eh2 = 'km') {
answer.innerHTML = eval(rnum1 + op + rnum2 * 1000);
} else {
if (eh = 'km') {
if (eh2 = 'km') {
answer.innerHTML = eval(rnum1 * 1000 + op + rnum2 * 1000);
} else {
answer.innerHTML = eval(rnum1 + op + rnum2)
};
}
}
}
}
}
<p> <label id="num1"> </label> <label id="e1"> </label>
<label id="operator"> </label>
<label id="num2"> </label> <label id="e2"> </label> =
<label id="answer">m </label> </p>
<button onclick="F1()"> New </button>
倚天杖
MYYA
相关分类