'Creating a simple Javascript Pin code validation

I'm trying to create a pin validation in javascript. I want to ask the user for a pin and they input 1234. If they enter the wrong pin 3 times I want it to say the message "incorrect pin. contact bank". Thanks for any help

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;
  }

}


Solution 1:[1]

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;
  }
}

Solution 2:[2]

Use a while loop to keep looping until either the attempts run out or pinCorrect is 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');
}

Solution 3:[3]

Perhaps use recursion for this


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
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cesare Polonara
Solution 2 Emiel Zuurbier
Solution 3