samedi 12 juin 2021

Javascript if statement does not meet other condition when the correct number is entered

I'm having an issue with my if statement. When I enter a number less than 16 it will still redirect to the lottery website. I'm not sure what I've missed or done wrong in my javascript code so any help is appreciated!

Javascript:

const button = document.querySelector(".image-button");
const age = 16;

function checkAge() {
    button.addEventListener('click', function() {
        prompt("What is your age?");
        if (age >= 16) {
            window.location.assign("https://www.national-lottery.co.uk/")
            alert("You are eligible to play");
        } else if (age < 16) {
            alert("You are not eligible to play");
        }
    });
}

checkAge();

Html:

<body>

<div class="image-container">
     <img src="lotterylogo.png" class="loto-image">
     <div class="image-title">
          <p>Are you eligible to play the national lottery?</p>
     </div>
     <div class="image-button">
          <button>Check</button>
     </div> 
</div>

<script src="script.js"></script>

</body>

Aucun commentaire:

Enregistrer un commentaire