[英]How to verify multiple OTPs using the prompt in browser?
I want to use the snippet below to verify multiple OTPs
. 我想使用下面的代码片段来验证多个
OTPs
。 If one OTP is correct, the user should be asked the next question. 如果一个OTP正确,则应询问用户下一个问题。 After 4 questions, show greetings.
4个问题后,致以问候。
var question = prompt('Who shot Abraham Lincoln?');
switch (question) {
case 'john wilkes booth':
case 'John Booth':
case 'John Wilkes Booth':
alert("That\'s Right!");
window.location.href = 'q2.html';
break;
default:
alert("Sorry, that\'s not right.");
alert('Please try again');
history.refresh();
break;
}
Need help to re-build the code above. 需要帮助来重新构建上面的代码。
Solution below 解决方案如下
var verificationStatus = 'unverified'; function questionnaire(questions, answers) { // Proceed only if # of questions and answers are equal if (questions && answers && questions.length === answers.length) { questions.forEach(function(question, index) { // Prompt only if verificationStatus has not been marked false already if (verificationStatus !== false) { var userInput = prompt(question); switch (userInput) { case answers[index]: verificationStatus = true; break; default: verificationStatus = false; break; } } }); } if (verificationStatus) { alert('Greetings, Verification Successful'); } else { alert('Sorry, Verification Failed'); } } // Please note # of questions and answers must be equal questionnaire(['Q1', 'Q2', 'Q3', 'Q4'], ['1', '2', '3', '4']);
Behaviour 行为
1, 2, 3, 4
respectively. 1, 2, 3, 4
。 Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.