jeudi 27 juin 2019

How to get function "X" to load only when "const pass == 'password'"? [duplicate]

This question already has an answer here:

I'm learning how to make prompts in js so when user types for example: "123" it tells that number is greater than 0, less than 0 or equal to 0, but only when user types correct password in (only when password is "number"). Otherwise it will tell "password incorrect".

I've tried googling but most of codes are in php but I just want simple js code without using servers. I'm trying for almost and hour now, different ID's, functions etc. I have no idea how to fix this.

JAVASCRIPT:

const haslo = document.querySelector('#password');
const btn = document.querySelector('button');

btn.addEventListener('click', function(){
    if(haslo=='liczba')
        function oknoPrompt(){
            const numbers=prompt('Podaj liczbe calkowita','12345');

            if(numbers < '0'){
                alert('ujemna');
            }else if(numbers > '0'){
                alert('dodatnia');
            }
            else if(numbers == '0'){
                alert('zero');
            }       
        }
    else {
        alert('incorrect pass')
    }
});

HTML:

<body>
<form>
    <input type="text" id="password">
    <button>przycisk</button>
</form>

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

I expect the function which tells if number is greater/lower/equal to only work when password = 'liczba'. But when I type "liczba" it shows "incorrect pass".

Aucun commentaire:

Enregistrer un commentaire