I will try to explain it better. I rewrote a code, to make it better as an example. As you can see I have an app to generate a number, but to generate correct number I have to click 5-15 times on a button, due to random and if statement. I want to ask how to make a process to skip incorrect numbers and give an actual answers ( which took 5-15 clicks ) only in one click. I can give more information, if you didn't understand me. Thank you very much!
*video: https://vimeo.com/251109159
function getnumber() {
var input = document.IX.onetoThou.value;
var firstNum = input[0];
var ranNumb = Math.floor(Math.random()*(99-00+1)+00);
var ans = "you are right!";
var newNumber = firstNum + ranNumb;
if ( newNumber % 5 == 0){
document.getElementById("newnumber").innerHTML = newNumber+" "+ans;
}
}
You can wrap the generation of your number inside a do while loop:
function getnumber() {
var input = document.IX.onetoThou.value;
var firstNum = input[0];
var ans = "you are right!";
var newNumber;
do {
var ranNumb = Math.floor(Math.random()*(99-00+1)+00);
newNumber = firstNum + ranNumb;
} while (newNumber % 5 !== 0);
document.getElementById("newnumber").innerHTML = newNumber+" "+ans;
}
不是在[0,100)中生成随机整数并检查它是否为5的倍数,而是在[0,20]中生成随机整数并将其乘以5.这样可以保证结果为5的倍数。在0到100之间。
var ranNumb = Math.floor(Math.random() * 20) * 5;
hope this might help. You have to use if-else
rather then if
conditional statement
function getnumber() {
var input = document.IX.onetoThou.value;
var firstNum = input[0];
var ranNumb = Math.floor(Math.random()*(99-00+1)+00);
var ans = "you are right!";
var newNumber = firstNum + ranNumb;
if ( newNumber % 5 == 0){
document.getElementById("newnumber").innerHTML = newNumber+" "+ans;
}else{
document.getElementById("newnumber").innerHTML = "You are wrong";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.