Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sound button duplication #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Project Name : Tic-Tac-Toe

## Issue

**Sound Button Duplication #132**

If you win the game and undo the last move, then win again, the sound button duplicates itself.

## Fork Repo Link : https://github.com/abhiram-shaji/tictactoe/tree/fix-sound-button-duplication

## Solution

The solution involves modifying the `checkWinner` function to check if the sound button already exists before creating a new one. This prevents the duplication of the sound button.

### Code Modification

```javascript
const checkWinner = () => {
// Existing code...

if (loss1 == 1 || loss2 == 1 || draw1 == 1 || draw2 == 1) {
// Check if the button already exists
if (!document.getElementById("btn-sound")) {
var btn = document.createElement("button");
btn.id = "btn-sound";
btn.className = "btn-sound";
btn.innerHTML = "<i class='fa fa-volume-up' aria-hidden='true'></i>";
btn.onclick = muteAudio;
document.getElementsByClassName("div-end-of-game")[0].appendChild(btn);
}
}
};
```

## Reflection

With the help of this contribution the app no multiplies sound button upon winning and retrying the last move.
17 changes: 11 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ const checkWinner = () => {
const winner_statement = document.getElementById("winner");
const audio = document.querySelector("audio");



if (res == "O") {
if (gameMode == 1) winner_statement.innerText = "Player Won"; // Single player mode
else winner_statement.innerText = "Player 1 Won"; // 2 player mode
Expand Down Expand Up @@ -282,12 +284,15 @@ const checkWinner = () => {
document.getElementById("draw1").innerText = temp5;
document.getElementById("draw2").innerText = temp6;

if (loss1 == 1 || loss2 == 1 || draw1 == 1 || draw2 == 1) { //when the game ends, I create and add a button in the 'div-end-of-game' div
var btn = document.createElement("button");
btn.className = "btn-sound";
btn.innerHTML = "<i class='fa fa-volume-up' aria-hidden='true'></i>";
btn.onclick = muteAudio;
document.getElementsByClassName("div-end-of-game")[0].appendChild(btn);
if (loss1 == 1 || loss2 == 1 || draw1 == 1 || draw2 == 1) {
// To check if the btn already exist
if (document.getElementsByClassName("btn-sound").length === 0) {
var btn = document.createElement("button");
btn.className = "btn-sound";
btn.innerHTML = "<i class='fa fa-volume-up' aria-hidden='true'></i>";
btn.onclick = muteAudio;
document.getElementsByClassName("div-end-of-game")[0].appendChild(btn);
}
}
};

Expand Down