Код: Выделить всё
// Add click event listeners to red balls (1 point)
const redBalls = document.querySelectorAll('.ball-img[src*="red"]');
redBalls.forEach(ball => {
ball.addEventListener('click', function() {
const column = this.closest('.col-4');
const isPlayerOneColumn = column.contains(document.getElementById('player-one-name'));
if ((activePlayer === 'player-one' && isPlayerOneColumn) ||
(activePlayer === 'player-two' && !isPlayerOneColumn)) {
currentBreak += 1;
document.getElementById('current-break').textContent = currentBreak;
} else {
console.log("Not your turn!");
}
});
});
// Add click event listeners to yellow balls (2 points)
const yellowBalls = document.querySelectorAll('.ball-img[src*="yellow"]');
yellowBalls.forEach(ball => {
ball.addEventListener('click', function() {
const column = this.closest('.col-4');
const isPlayerOneColumn = column.contains(document.getElementById('player-one-name'));
if ((activePlayer === 'player-one' && isPlayerOneColumn) ||
(activePlayer === 'player-two' && !isPlayerOneColumn)) {
currentBreak += 2;
document.getElementById('current-break').textContent = currentBreak;
} else {
console.log("Not your turn!");
}
});
});
Подробнее здесь: https://stackoverflow.com/questions/794 ... javascript