简单 JavaScript 密码检查器不会在第一次迭代后循环
Simple JavaScript password checker won't loop after the first iteration
至少,我 认为 迭代在第一个循环后停止。在发送错误消息之前输入不正确的密码时,我试图让我的密码循环 3 次。当我输入一个不正确的密码并单击我的提交按钮时,迭代停止。编码新手,所以如果我做错了什么,请告诉我。
我的 JS(按钮 on-click
功能):
var password = "password";
var notif = document.getElementById("notif");
var entryCount = 0;
var entryLimit = 3;
var error = false;
function inputPW(){
for(i = 0; i < 3; i++){
notif.innerHTML = "";
if(passwordInp.value === password){
notif.innerHTML = "You got it!";
break;
}
else if(i < 3){
notif.innerHTML = "Incorrect password. Please try again.";
}
else{
notif.innerHTML = "Password limits exceeded.";
}
}
}
我的HTML(仅正文):
<h1>Password Getter</h1>
<p>Password:</p>
<input id = "passwordInp" type="text" placeholder="Password" autofocus>
<button id="enterBtn" type="button" onclick="inputPW()">Submit</button>
<p id="notif"></p>
<script src="JSscript.js"></script>
for 循环一次运行相同的代码,而不是等待用户的输入。因为输入在 运行 时没有改变,所以它只运行一次。而是在用户单击提交按钮并递增全局变量时调用该函数。
var password = "password";
var notif = document.getElementById("notif");
var passwordInp = document.getElementById("passwordInp");
var entryCount = 0;
var entryLimit = 3;
var error = false;
var i = 0;
function inputPW(){
notif.innerHTML = "";
if(passwordInp.value === password){
notif.innerHTML = "You got it!";
break;
else if(i < 3){
notif.innerHTML = "Incorrect password. Please try again.";
}
else{
notif.innerHTML = "Password limits exceeded.";
}
}
}
//these constants won't change during execution
const LIMIT = 3;
const PASSWORD = 'password';
let entryCount = 0; // a global variable
let authorized = true; //can we still try?
function handleInput(inp){
if(!authorized){
return; //we do nothing if we're not authorized anymore
}
const matches = inp == PASSWORD;
if(matches){
//do 'success' thing
} else if(entryCount < LIMIT){
entryCount++;
//do 'not success' thing
} else{
authorized = false;
//do 'unauthorized' thing
}
}
<h1>Password Getter</h1>
<p>Password:</p>
<input id = "passwordInp" type="text" placeholder="Password" autofocus>
<button id="enterBtn" type="button" onclick="handleClick(passwordInp.value)">Submit</button>
<p id="notif"></p>
<script src="JSscript.js"></script>
至少,我 认为 迭代在第一个循环后停止。在发送错误消息之前输入不正确的密码时,我试图让我的密码循环 3 次。当我输入一个不正确的密码并单击我的提交按钮时,迭代停止。编码新手,所以如果我做错了什么,请告诉我。
我的 JS(按钮 on-click
功能):
var password = "password";
var notif = document.getElementById("notif");
var entryCount = 0;
var entryLimit = 3;
var error = false;
function inputPW(){
for(i = 0; i < 3; i++){
notif.innerHTML = "";
if(passwordInp.value === password){
notif.innerHTML = "You got it!";
break;
}
else if(i < 3){
notif.innerHTML = "Incorrect password. Please try again.";
}
else{
notif.innerHTML = "Password limits exceeded.";
}
}
}
我的HTML(仅正文):
<h1>Password Getter</h1>
<p>Password:</p>
<input id = "passwordInp" type="text" placeholder="Password" autofocus>
<button id="enterBtn" type="button" onclick="inputPW()">Submit</button>
<p id="notif"></p>
<script src="JSscript.js"></script>
for 循环一次运行相同的代码,而不是等待用户的输入。因为输入在 运行 时没有改变,所以它只运行一次。而是在用户单击提交按钮并递增全局变量时调用该函数。
var password = "password";
var notif = document.getElementById("notif");
var passwordInp = document.getElementById("passwordInp");
var entryCount = 0;
var entryLimit = 3;
var error = false;
var i = 0;
function inputPW(){
notif.innerHTML = "";
if(passwordInp.value === password){
notif.innerHTML = "You got it!";
break;
else if(i < 3){
notif.innerHTML = "Incorrect password. Please try again.";
}
else{
notif.innerHTML = "Password limits exceeded.";
}
}
}
//these constants won't change during execution
const LIMIT = 3;
const PASSWORD = 'password';
let entryCount = 0; // a global variable
let authorized = true; //can we still try?
function handleInput(inp){
if(!authorized){
return; //we do nothing if we're not authorized anymore
}
const matches = inp == PASSWORD;
if(matches){
//do 'success' thing
} else if(entryCount < LIMIT){
entryCount++;
//do 'not success' thing
} else{
authorized = false;
//do 'unauthorized' thing
}
}
<h1>Password Getter</h1>
<p>Password:</p>
<input id = "passwordInp" type="text" placeholder="Password" autofocus>
<button id="enterBtn" type="button" onclick="handleClick(passwordInp.value)">Submit</button>
<p id="notif"></p>
<script src="JSscript.js"></script>