I have the following HTML code, with the following jQuery code, referencing a constructor called Game, and a function called roll. I pass a different argument to each of the even handlers, based on which button you press in the HTML file, however I was wondering how you would refactor this? Many thanks
HTML Code:
<section>
<h1>Score:</h1>
<h1 id="score"></h1>
<p>
<button id="1">1</button>
<button id="2">2</button>
<button id="3">3</button>
<button id="4">4</button>
<button id="5">5</button>
<button id="6">6</button>
<button id="7">7</button>
<button id="8">8</button>
<button id="9">9</button>
<button id="10">10</button>
</p>
</section>
jQuery Code:
$(document).ready(function() {
var game = new Game();
$('#score').text(game.score());
$('#1').on('click', function() {
game.roll(1);
$('#score').text(game.score());
});
$('#2').on('click', function() {
game.roll(2);
$('#score').text(game.score());
});
$('#3').on('click', function() {
game.roll(3);
$('#score').text(game.score());
});
$('#4').on('click', function() {
game.roll(4);
$('#score').text(game.score());
});
$('#5').on('click', function() {
game.roll(5);
$('#score').text(game.score());
});
$('#6').on('click', function() {
game.roll(6);
$('#score').text(game.score());
});
$('#7').on('click', function() {
game.roll(7);
$('#score').text(game.score());
});
$('#8').on('click', function() {
game.roll(8);
$('#score').text(game.score());
});
$('#9').on('click', function() {
game.roll(9);
$('#score').text(game.score());
});
$('#10').on('click', function() {
game.roll(10);
$('#score').text(game.score());
});
});