【问题标题】: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;
}
需要帮助重新构建上面的代码。
【问题讨论】:
标签:
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。
- 如果在任何时候给出了错误的答案,不会再问任何问题。
- 最后会显示一条消息(问候)。
希望对你有帮助!