Press virtual keyboard buttons according to password

0

I made a virtual keyboard that generates the numbers and returns in json, for example:

            [0] => Array
                (
                    [a] => 1
                    [b] => 5
                )

            [1] => Array
                (
                    [a] => 6
                    [b] => 7
                )

            [2] => Array
                (
                    [a] => 4
                    [b] => 9
                )

            [3] => Array
                (
                    [a] => 8
                    [b] => 2
                )

            [4] => Array
                (
                    [a] => 3
                    [b] => 0
                )

The above code generates the numbers in the following format: A or B For example: Press this button if your password is between A or B.

And along with this code, it has a variable that defines the person's password, in which case the password is 011232. The values that the virtual keyboard gives are random.

I need a code that takes the values between the 5 buttons and check button by button, if the variable password is equal to one of two values (A and B)

Thank you in advance.

    
asked by anonymous 16.05.2017 / 04:33

1 answer

0

I've been in JS

var teclas = [{"a":1,"b":5},{"a":6,"b":7},{"a":4,"b":9},{"a":8,"b":2},{"a":3,"b":0}];
var password = '011232';
var check = 0;

function checkPassword(e){
    var index = parseInt(this.getAttribute('data-index'));
    var arr = [parseInt(teclas[index].a), parseInt(teclas[index].b)];
    if(arr.indexOf(parseInt(password[check])) != -1){
        check++;
    }else{
        check = 0;
        alert('Password Error');
    }

    if(check >= password.length){
        alert('Password OK');
    check = 0;
    }
}

function createButton(i){

    var o = teclas[i];

    var button = document.createElement('input');
    button.type = 'button';
    button.value = o.a+','+o.b;
    button.setAttribute('data-index', i);
    button.addEventListener('click', checkPassword);

    return button;
}

var teclado = document.getElementById('teclado');
for(var i=0; i < teclas.length; i++){
    teclado.appendChild(createButton(i));
}
<div id="teclado">
</div>
    
16.05.2017 / 15:14