[英]How do I make the first number bigger?
I'm working on a game for my kids and I may end up posting this game online at a later date if it's fully functional.我正在为我的孩子们开发一款游戏,如果它功能齐全,我可能会在以后在线发布这款游戏。 But I'm working on a math deal.
但我正在做一笔数学交易。 Basically it generates 2 random numbers 0-4 and they have to subtract and choose the correct answer.
基本上它会生成 2 个随机数 0-4,他们必须减去并选择正确的答案。 How do I go about making sure the first number is always bigger?
我如何确保第一个数字总是更大?
Here's my HTML:这是我的 HTML:
<header>
<div class="container">
<h1>Math 4 KIDS</h1>
<ul>
<li class=""><a href="add.html">Add +</a></li>
<li class="current"><a href="subtract.html">Subtract -</a></li>
</ul>
</div>
</header>
<div class="wrapper">
<div class="equation">
<h1 id="num1">1</h1>
<h1 style="color: #2ab7ca;">-</h1>
<h1 id="num2">1</h1>
<h1 style="color: #fe4a49;">=</h1>
<h1 style="color: gray;">?</h1>
</div>
<div class="answer-options">
<h1 id="option1">1</h1>
<h1 id="option2">2</h1>
<h1 id="option3">3</h1>
</div>
</div>
And my JS:还有我的 JS:
const option1 = document.getElementById('option1');
const option2 = document.getElementById('option2');
const option3 = document.getElementById('option3');
const audio = document.getElementById('myAudio');
var answer = 0;
function generate_equation() {
var num1 = Math.floor(Math.random() * 5);
var num2 = Math.floor(Math.random() * 5);
var dummyAnswer1 = Math.floor(Math.random() * 6);
var dummyAnswer2 = Math.floor(Math.random() * 6);
var allAnswers = [];
var switchAnswers = [];
answer = num1 - num2;
document.getElementById('num1').innerHTML = num1;
document.getElementById('num2').innerHTML = num2;
allAnswers = [answer, dummyAnswer1, dummyAnswer2]
for(i = allAnswers.length; i--;) {
switchAnswers.push(allAnswers.splice(Math.floor(Math.random() * (i + 1)), 1)[0]);
}
option1.innerHTML = switchAnswers[0];
option2.innerHTML = switchAnswers[1];
option3.innerHTML = switchAnswers[2];
}
option1.addEventListener("click", function() {
if(option1.innerHTML == answer) {
generate_equation();
} else {
audio.play();
}
});
option2.addEventListener("click", function() {
if(option2.innerHTML == answer) {
generate_equation();
} else {
audio.play();
}
});
option3.addEventListener("click", function() {
if(option3.innerHTML == answer) {
generate_equation();
} else {
audio.play();
}
});
generate_equation()
Use a conditional statement to check whether num1 is smaller than num2, if yes, switch them around使用条件语句检查 num1 是否小于 num2,如果是,则切换它们
So insert the following所以插入以下内容
if (num1 < num2){
var tnum=num1;
num1=num2;
num2=tnum;
}
after the line行后
var num2 = Math.floor(Math.random() * 5);
one way you could do this would be to generate the second number first, generate a number for how much higher num1 should be, then add it back in. That way you are essentially randomly choosing a number between your max (4 in this case) and the second number.你可以这样做的一种方法是首先生成第二个数字,生成一个数字 num1 应该高多少,然后将其重新添加。这样你基本上是在你的最大值之间随机选择一个数字(在这种情况下为 4)和第二个数字。
Here's an example:这是一个例子:
// Made max a variable for clarity in example
var randomMax = 5
// Generate random number between 0 and [randomMax] - 1
var num2 = Math.floor(Math.random() * randomMax);
// Generate the amount to add to the second number, allowing for the number
// to be equal to the first. If num2 is 3, num1 can only be 0 or 1 but if
// num2 is 0, it may be 1,2,3, or 4
var num1 = Math.floor(Math.random() * (randomMax - num2));
// Add num2 to num1. If num2 is 2, then num1 can only be 2,3, or 4
num1 += num2
You can shorten it as such if desired如果需要,您可以将其缩短
var randomMax = 5
var num2 = Math.floor(Math.random() * randomMax);
var num1 = Math.floor(Math.random() * (randomMax - num2)) + num2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.