Write createLogin(password, maxAttempts) that returns a function: correct passwords log in, wrong ones count as failures, and after maxAttempts failures the account is locked even for the right password.
Solution
main.js
function createLogin(password, maxAttempts = 3) {
let failures = 0;
return (guess) => {
if (failures >= maxAttempts) return "Account locked";
if (guess === password) { failures = 0; return "Welcome!"; }
failures++;
const left = maxAttempts - failures;
return left > 0 ? `Wrong password (${left} left)` : "Account locked";
};
}
const login = createLogin("lf@2026", 3);
for (const guess of ["abc", "lf@2026", "123", "456", "789", "lf@2026"]) {
console.log(guess.padEnd(8), login(guess));
}
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!