我的代碼有問題,因為我想在單擊“新建”onclick 按鈕時顯示和列印亂數、運算子及其答案。請幫我。謝謝
<!DOCTYPE html>
<html>
<title>MATH FLASHCARD 1.0</title>
<head>
<link rel="stylesheet" href="style.css">
<script>
var operators = [{
sign: " ",
method: function(num1,num2){ return num1 num2; }
},{
sign: "-",
method: function(num1,num2){ return num1 - num2; }
}];
var selectedOperator = Math.floor(Math.random()*operators.length);
operators[selectedOperator].sign //this will give you the sign
operators[selectedOperator].method(rnum1, rnum2) //this will give you the answer
function New()
{
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
}
</script>
</head>
<h2>MATH FLASHCARDS V1.0</h2>
<body>
<div>
<div id="num1"></div><!--end of num1-->
<div id="num2"></div><!--end of num2-->
<div id="operators"></div><!--end of operator-->
<div id="answer"></div><!--end of answer-->
<button onclick="New()">New</button>
uj5u.com熱心網友回復:
學習閱讀控制臺中的錯誤。在這種情況下,錯誤清楚地解釋了您的代碼到底出了什么問題。
首先,宣告你的變數。
然后將確定答案的代碼放入函式中,以便實際呼叫它。
然后呼叫該函式并對輸出進行實際操作。我只是在控制臺中輸入。
var num1, num2, rnum1, rnum2, sign, answer;
var operators = [{
sign: " ",
method: function(num1, num2) {
return num1 num2;
}
}, {
sign: "-",
method: function(num1, num2) {
return num1 - num2;
}
}];
New();
function New() {
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
var selectedOperator = Math.floor(Math.random() * operators.length);
sign = operators[selectedOperator].sign //this will give you the sign
answer = operators[selectedOperator].method(rnum1, rnum2) //this will give you the answer
console.log('sign:', sign, 'answer:', answer);
}
<h2>MATH FLASHCARDS V1.0</h2>
<div>
<div id="num1"></div>
<!--end of num1-->
<div id="num2"></div>
<!--end of num2-->
<div id="operators"></div>
<!--end of operator-->
<div id="answer"></div>
<!--end of answer-->
<button onclick="New()">New</button>
</div>
uj5u.com熱心網友回復:
也許這就是您正在尋找的結果:
let operators = [{
sign: " ",
method: function(num1, num2) {
return num1 num2;
}
}, {
sign: "-",
method: function(num1, num2) {
return num1 - num2;
}
}];
function New() {
const num1 = document.getElementById("num1");
const num2 = document.getElementById("num2");
const operatorEl = document.getElementById("operators");
const answerEl = document.getElementById("answer");
const rnum1 = Math.floor((Math.random() * 10) 1);
let rnum2;
do{
rnum2 = Math.floor((Math.random() * 10) 1)
}while(rnum2 > rnum1)
const selectedOperator = Math.floor(Math.random() * operators.length);
const operator = operators[selectedOperator] //this will give you the sign
const answer = operator.method(rnum1, rnum2) //this will give you the answer
num1.innerHTML = rnum1
num2.innerHTML = rnum2
operatorEl.innerHTML = operator.sign;
answerEl.innerHTML = answer
}
<h2>MATH FLASHCARDS V1.0</h2>
<div>
<div id="num1"></div>
<!--end of num1-->
<div id="num2"></div>
<!--end of num2-->
<div id="operators"></div>
<!--end of operator-->
<div id="answer"></div>
<!--end of answer-->
<button onclick="New()">New</button>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375512.html
標籤:javascript
