Codecademy Practice

Rock, Paper, Scissors - Part 3

The next part is about adding audio effects. For this we are going to use the audio API. This API creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.

We are going to use it in it’s offscreen version.

Step by step instructions

Download the audio from here.

HTML

No changes.

CSS

No changes.

JS

We want to change the audio we play depending on the result of playing the game. For this we are going to use the audio API to create an audio element like this:

var audio = new Audio(audioFile);

and then we can play it like this:

audio.play();

When you are finished, look at your code. The if / else conditions are starting to get a bit messy. How can we clean the code?