Hello everyone, I decided to make a counter for playing table tennis here at my leisure. Tasks that he must perform: - Increase the score when you click on the appropriate button - When you click on the "Party" button, reset the score - Display whose current feed So now, I did the first 2 points, but there is a problem with the third one. In tennis, the serve goes to another player when the score is even, that is, when the player serves 2 feeds, for example, when the score is 2-0, 1-1, 8-6, 7-5, etc. I have 2 ball images that have visibility = 'hidden', and when the bill amount is divided by 2, then they have to change the properties to visibility = 'hidden' and visibility = 'visible' This is how I implemented it:
var $leftScore = document.getElementById('lscore'); var $rightScore = document.getElementById('rscore'); leftSum = 0; rightSum = 0; function lcounter() { $leftScore.innerHTML = ++leftSum;; onUpdate(); } function rcounter() { $rightScore.innerHTML = ++rightSum; onUpdate(); } function reset() { $leftScore.innerHTML = leftSum = 0; $rightScore.innerHTML = rightSum = 0; document.getElementById('lBall').style.visibility = 'visible'; document.getElementById('rBall').style.visibility = 'hidden'; } function onUpdate() { var x = (+leftSum + +rightSum) % 2 == 0; var y = (+leftSum + +rightSum); if ((+leftSum + +rightSum) % 2 == 0) { document.getElementById('lBall').style.visibility = 'visible'; document.getElementById('rBall').style.visibility = 'hidden'; // sum % 2 = 1 } else { document.getElementById('rBall').style.visibility = 'visible'; document.getElementById('lBall').style.visibility = 'hidden'; // sum % 2 = 0 } } * { margin: 0; padding: 0; box-sizing: border-box; } body { background-color: #0d3573; color: white; font-size: 40px; } img { width: 100%; } .main { height: 100vh; max-height: 100%; background-position: center; margin: 0 auto; display: flex; flex-direction: column; justify-content: space-around; } .top, .bottom { display: flex; justify-content: space-around; align-items: center; min-height: 159px; } .bottom { justify-content: space-around; } .section { display: flex; justify-content: center; align-items: center; } .left_score { color: red; background-color: skyblue; width: 80px; height: 80px; text-align: center; } .right_score { color: blue; background-color: skyblue; width: 80px; height: 80px; } .ball { width: 80px; height: 80px; background-color: orange; visibility: hidden; margin: 0 auto; border-radius: 50%; } .ball img { display: block; text-align: center; } button { background-color: antiquewhite; width: 80px; height: 80px; font-size: 40px; } #reset{ font-size: 15px; } #rBall{ padding-right: 10px; } #lBall{ padding-left: 10px; } <div class="main"> <div class="top"> <div class="section left_ball"> <div class="ball" id="lBall" style="visibility:visible"> <div src="img/ball.png" alt="ball"> </div> </div> <div class="section left_score score" id="lscore">0</div> <div class="section right_score score" id="rscore">0</div> <div class="section right_ball"> <div class="ball" id="rBall"> <div src="img/ball.png" alt="ball"> </div> </div> </div> <div class="bottom"> <div class="section left plus" id="leftPlus"> <button id="lbutton" onclick="lcounter()" class="myBtn">+</button> </div> <div class="section reset" id="rightPlus"> <button id="reset" onclick="reset()">партия</button> </div> <div class="section right plus" id="rightPlus"> <button id="rbutton" onclick="rcounter()" class="myBtn">+</button> </div> </div> </div> But the problem is that the properties change with each increase in the score, and not just when the score is divided by 2. I think there is a little wrong logic here. 2nd day I can’t figure out how to properly implement this moment :( This is the page