Secret code combination

by Kenneth Luplau-Brøgger

HTML

<a href="javascript:void(0)" data-button-id="1">1</a>
<a href="javascript:void(0)" data-button-id="2">2</a>
<a href="javascript:void(0)" data-button-id="3">3</a>
<a href="javascript:void(0)" data-button-id="4">4</a>
<a href="javascript:void(0)" data-button-id="5">5</a>

SCSS

a {
    display: block;
    float: left;
    width: 100px;
    height: 100px;
    line-height: 100px;
    background: black;
    color: white;
    font-size: 50px;
    text-align: center;
    
    & + a {
        margin-left: 10px;
    }
}

JavaScript

var secretCode = {
    secretCode: [2,3,5],
    numberOfCorrectKeys: 0,
    test: function(e) {
        var key = $(e.target).data("buttonId");
    
        if (key == this.secretCode[this.numberOfCorrectKeys]) {
            this.numberOfCorrectKeys++;
        } else {
            this.numberOfCorrectKeys = 0;
        }
        
        if (this.numberOfCorrectKeys == this.secretCode.length) {
            alert("Correct code");
        }
    }
}

$("a").click(function(e) {
    secretCode.test(e);
});