简体   繁体   English

如何使用浏览器中的提示来验证多个OTP?

[英]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 行为

  • The snippet above asks 4 questions, to which answers are 1, 2, 3, 4 respectively. 上面的代码段问了4个问题,答案分别是1, 2, 3, 4
  • If at any point, wrong answer is given, no more questions are asked. 如果在任何时候给出错误的答案,都不会再提出任何问题。
  • A message (greetings) is displayed at the end. 最后会显示一条消息(问候)。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM