<input type="number" id="answer"/>
<script>
var Game = (function() {
let rightAnswer;
return {
generateNewRightValue: function() {
rightAnswer = Math.floor(Math.random() * 31);
return this;
},
check: function(answer) {
return +answer == rightAnswer;
}
};
})().generateNewRightValue();
document.querySelector('#answer').addEventListener('input', function() {
if (!Game.check(this.value))
return void alert('Wrong');
Game.generateNewRightValue();
alert('Right');
});
</script>