我正在嘗試在 javascript 中創建 pin 驗證。我想向用戶詢問密碼,他們輸入 1234。如果他們輸入錯誤的密碼 3 次,我希望它說“密碼不正確。聯系銀行”的訊息。謝謝你的幫助
function pinCode() {
const Pin = "1234"; {}
const name = prompt("Please enter your full name");
const user_Attempt = prompt("Please enter your PIN number to access your bank account");
if (user_Attempt = Pin) {
alert("Welcome" name);
} else {
alert("Incorrect pin. Please contact your bank");
return;
}
}
uj5u.com熱心網友回復:
const pin = "1234";
let attemptsLeft = 3
const name = prompt("Please enter your full name");
for (attemptsLeft; attemptsLeft >= 0; attemptsLeft--) {
if (attemptsLeft === 0) {
alert("Incorrect pin. Please contact your bank");
break;
}
const userAttempt = prompt(`Please enter your PIN number to access your bank account. You have ${attemptsLeft} left.`);
if (userAttempt === pin) {
alert("Welcome " name);
break;
}
}
uj5u.com熱心網友回復:
使用while回圈保持回圈,直到attempts用完或pinCorrectis true。
const correctPin = '1234';
const maxAttempts = 3;
let attempts = 0;
let pinCorrect = false;
while (attempts < maxAttempts && !pinCorrect) {
const pinInput = prompt(`Please enter your PIN number to access your bank account. ${maxAttempts - attempts} attempts left.`);
pinCorrect = pinInput === correctPin;
attempts ;
}
if (!pinCorrect) {
alert('Incorrect pin. Please contact your bank');
} else {
alert('Pin accepted');
}
uj5u.com熱心網友回復:
也許為此使用遞回
var incorrectCount = 0;
function pinCode() {
if (incorrectCount != 3) {
// prompts here
if (user_Attempt === Pin) {
// do stuff
} else {
incorrectCount = 1;
pinCode();
}
} else {
// code for too many incorrect tries
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459503.html
標籤:javascript
