So now that we know which key was pressed and how we can get its innerHTML, how can we play a different 1
sound depending on which key was pressed? 2
Well you might think to the if else statements that we learned in our introduction to Javascript modules, 3
but there's actually seven keys in total, 4
so we would have to write quite a few if else statements. 5
Now in this area it would actually be easier to write what's called a switch statement which, just like 6
a railroad switch, 7
it will take the code down a different track depending on the value of a variable. 8
So let's delete this line of code and let's start writing our switch statements. 9
Now inside Atom, as soon as you start writing the word switch, then our auto suggest will helpfully give 10
you the skeleton of a Javascript switch statement. 11
So if you hit enter then it'll give you the bare bones of what it should look like. 12
So you can see the keyword here, instead of if, is switch. 13
And then afterwards we have a set of parentheses which includes the expression or the thing that we're 14
going to switch on. 15
And in our case, we're going to switch on the letter that is inside the innerHTML of each of these 16
buttons. 17
So let's create a variable that holds that value. 18
So we'll call it buttonInnerHTML, and we'll set it to equal this, 19
so remember that's the button that triggered the event, .innerHTML. 20
So now let's switch the button's innerHTML, and, depending on the case, we'll tell it to do something different. 21
So the first case is if that was equal to w. 22
So remember our first button has an innerHTML of the letter w. 23
So if it was the letter w, 24
then we're going to tell it to play the tom 1 drum. 25
So I'm going to take that code that I commented out and then put it in here so that it will play the 26
first drum. 27
Now after the first case is done, then there is a break statement, which tells the code to exit the switch 28
statement and continue with the rest of the code. 29
Now if buttonInnerHTML didn't happen to be equal to w, then it could be equal to something else. 30
So in that case we would have to specify the next likely scenario, which is what if the innerHTML was 31
a, i.e. the second button got clicked. 32
Well, in this case, we're going to play the next sound instead, and we're going to play the tom-2.mp3. 33
And once again we have to end our case with a break. 34
So the switch statement looks a little bit weird, because instead of having, say for example, an open parentheses 35
here and a closing parentheses here, it actually has a colon and a break keyword instead. 36
But just know that it's basically the same thing. Between the colon and the break keyword are 37
all the things we want the web site to carry out if the buttonInnerHTML was equal to this particular 38
case. 39
Now in this case, 40
instead of calling both of our sounds 41
audio, we should really give it a different name. 42
So why don't we call our first one the sound that it's going to make, 43
so tom1, and our second one tom2, after the sound file that's going to be played? 44
And remember if you're going to change it here then you have to change it here as well so that you know 45
that you're playing tom1 and you're playing tom2. 46
Now I've just gone in and added all of the remaining cases for the key s, d, j, k and l. 47
So now I've gone in and I've added all of the other remaining cases so that our web site knows what 48
to do if the s key was pressed or the d or the j, k, l. 49
Now the final case that we've got is actually the default case. 50
Now the default is kind of like the else statement at the end of an if. It covers all of the other scenarios 51
that we haven't specified. 52
What if the buttonInnerHTML wasn't w, a, s, d, and instead it was something like 1 or comma? 53
Well that's when the default will trigger. 54
Now even though in most cases the default will likely never be triggered, 55
it's still good practice to include it, as you can't always predict ahead of time what might go wrong 56
in the future, 57
right? 58
And the value could always be something that you didn't expect. 59
So let's just go ahead and add a console.log that logs the buttonInnerHTML 60
that triggered the default case. 61
So let's save, refresh our web site, and you can see now if I click on any of these buttons 62
you can see that we get a different sound for each button click. 63
That's pretty cool, right? Now 64
in the next lesson we're going to dig into keyboard events and see how we can use the keyboard to trigger 65
these sounds instead of having to press on the buttons. 66
So for all of that and more, I'll see you on the next lesson.