Suggestion:
var count = 0;
var p1_placar = document.getElementById('placar1'),
p2_placar = document.getElementById('placar2'),
p1_jogada = document.getElementById('campojogada1'),
p2_jogada = document.getElementById('campojogada2'),
side = document.getElementById('side');
side.onclick = function(){
executar1();
};
function executar1() {
document.getElementById('conte').value = count;
if (count % 2 == 0) jogarr(p1_jogada, p2_jogada, p1_placar);
else jogarr(p2_jogada, p1_jogada, p2_placar);
}
function jogarr(jogad, jogad2, plac) {
var girar06 = 1 + Math.round(Math.random() * 5);
var imgs = new Array(null, "side1.png", "side2.png", "side3.png", "side4.png", "side5.png", "side6.png");
var pontos = plac.value;
for (i = 1; i <= 6; i++) {
if (girar06 == i) side.src = imgs[i]
}
if (jogad.value == girar06) {
pontos++;
alert('Vc acertou');
} else {
alert('Você errou')
}
count++;
jogad.disabled = true;
jogad.style.backgroundColor = '#CCC';
jogad.value = "";
jogad2.disabled = false;
jogad2.style.backgroundColor = '#fff';
plac.value = pontos;
return count;
} // end jogarr
What I have changed:
- I removed the function within the function
- onclick usage in Javascript instead of HTML
- reorganized the code to avoid equal line repeats within if/Else
- corrected the error I had when using
plac = plac + 1;
and how the variable has a different name p1_placar
and/or p2_placar
were not updated.
Note: for this code to work on an HTML page you have to put an onload function, or put Javascript at the end of the HTML, before the tag </body>
I edited the question to be readable, see if I guessed well what you meant.
– Maniero
@mustache, thanks a friend.
– ropbla9