This is a beginner project; for this aspect I’m creating a simple password function that allows the user 3 attempts before exiting the program. The loop should end on ANY loop increment where the password is correct. However the loop works on first attempt if true but if the first attempt is wrong and the second attempt is correct the loop still continues and repeats the password prompt. I apologize if my explanation isn’t as detailed as required as I’m new here
Example:
const prompt = require("prompt-sync")();
const userpasswordkey = "Snow";
function password() {
let pwcount = 0;
while (pwcount < 2) {
let loginpassword = prompt("Enter Database password: ");
if (loginpassword === userpasswordkey) {
console.log("Login Complete");
return true;
// Exit the function early if the login succeeds
} else {
console.log("Try Again");
pwcount++;
}
}
console.log("password limit reached, Program Terminated.");
return false; //Return false if password attempts fail
}
password();
if (password()) {
//checks if password returned true
// console.clear();
console.log("Correct logic")
}
I have also tried using breaks and the loop still carries on