[英]Why does my previous function don't work when I add a new function? How can I fix this?
因此,generateOTP()应该给我一个随机数,它将显示在文本“ Enter the Number Above”上方,它可以工作,但是在我添加第二个函数后,它消失了。 该程序应该做的是显示一个随机数,然后在该数字与该随机数匹配时输入下一个数字,这将给出一条消息“您的答案与上述数字匹配”,否则您的答案与上述数字不匹配。“ 请帮忙
<html>
<head>
<title>Simulating One-Time-Password (OTP) or CAPTCHA</title>
<meta charset="UTF-8">
<meta name="description" content="Simulating One-Time-Password (OTP) or CAPTCHA">
<meta name="keywords" content="java, otp">
<meta name="author" content="sad">
<style>
</style>
<script>
function generateOTP() {
// Declare a digits variable
// which stores all digits
var digits = '0123456789';
let OTP = '';
for (let i = 0; i < 6; i++ ) {
OTP += digits[Math.floor(Math.random() * 10)];
}
return OTP;
}
document.write("OTP of 6 digits: ")
document.write( generateOTP() );
var ranNum = generateOTP();
function checkValue(){
var finalAnswer =
document.getElementById("answer").value;
if (ranNum== finalAnswer) {
document.write("Your answer matches the number above")
else{
document.write("Your answer does not match the number above")
}
}
}
</script>
</head>
<body>
<p> Enter the number above
<input id="answer" type="text"/>
</p>
<p>
<button onclick="checkValue()"> Display </button>
</p>
</body>
</html>
您可以在if
语句中使用else
关键字。 它应该是:
if (condition) {
// do something
} else {
// do something else
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.