【问题标题】:How to verify multiple OTPs using the prompt in browser?如何使用浏览器中的提示验证多个 OTP?
【发布时间】:2018-08-25 16:50:31
【问题描述】:

我想用下面的sn-p来验证多个OTPs。如果一个 OTP 正确,则应向用户询问下一个问题。 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;
}

需要帮助重新构建上面的代码。

【问题讨论】:

  • 与 OTP 验证相关的问题/答案如何?
  • 问题是人名,例如:“输入姓名 1 otp”

标签: javascript if-statement one-time-password


【解决方案1】:

以下解决方案

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']);

行为

  • 上面的sn-p问了4个问题,答案分别是1, 2, 3, 4
  • 如果在任何时候给出了错误的答案,不会再问任何问题。
  • 最后会显示一条消息(问候)。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2019-06-22
    • 2013-07-30
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多