> For the complete documentation index, see [llms.txt](https://docs.idew.org/code-trivia-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.idew.org/code-trivia-app/more/code-mod-examples/sound-add-sound-effects-on-answer-clicks.md).

# Add Sound Effects on Answer Clicks

Let's add some simple sound effects to your trivia game when a player answers a question.&#x20;

**1 - Find sound effects you would like to use or make your own.** Files of type *.mp3* are a good choice for web apps. You can download the two sounds below as a starting point.&#x20;

{% file src="/files/-LJu4TFW\_u8EaVlZBQsg" %}
sound\_correct.mp3
{% endfile %}

{% file src="/files/-LJu4clnrypoz6XcRfj-" %}
sound\_incorrect.mp3
{% endfile %}

**2 - Create a folder named `sounds` and place your sound files in it.** Pay close attention to your files and folder structure for you app. Below is an example of what yours should look like.

* index.html
* style.css
* code.js
* **sounds**
  * sound\_correct.mp3
  * sound\_incorrect.mp3

**3 - Declare variables for your sounds at the very top of your&#x20;*****code.js*****&#x20;file.** They should be above the `setup()` function. These lines create variables that will *represent* your sound files so that you can play them at a later point in your code. Also, since we are placing them at the top of your code and not inside any function, they are called *global variables*. This means we can access these variables inside any other function, which we will do next.

```javascript
var soundCorrect = new Audio("sounds/sound_correct.mp3");
var soundIncorrect = new Audio("sounds/sound_incorrect.mp3");
```

**4 - Place the code shown below inside your `onClickedAnswer( )` function to play the sounds at the appropriate time.** Notice how we are combining the text and sound feedback in our *if/else* statements inside the code brackets `{ }`. Your text feedback will likely be different. So adjust as needed.&#x20;

```javascript
  if (isCorrect) {
    $("#feedback").html(`Way to go!`).show();
    soundCorrect.play();
  }
  else {
    $("#feedback").html(`Better luck next time.`).show();
    soundIncorrect.play();
  }
```

That's it. Your game should now play your sound effects when a player answers a question.

{% hint style="info" %}
If you are using a longer sound file, like a song, you will want to know about the following two features.

**pause( )** - This method will pause your sound. For example, `soundCorrect.pause( );`

**currentTime** - This property can be used to reset the sound to the beginning. For example, `soundCorrect.currentTime = 0;`
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.idew.org/code-trivia-app/more/code-mod-examples/sound-add-sound-effects-on-answer-clicks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
