Welcome back in this lesson, we are going to make our code shorter.
And if you want, you can pause the video here and before I make some updates to the code, try it out
for yourself, I'll give you a hint.
One of the things that we are going to be doing is we're going to be making it shorter because we are
using the same values.
So Barch, Cougar, Lion, and we also have Barch, Cougar, Lion within those classes as well as within
the names of those elements.
So you can see that we do have an option here to really utilize this in a unique way where we can shorten
a lot of our code because there's a consistency there within the naming convention as well as the naming
convention of the classes.
So go ahead and try this out for yourself and I'll show you the solution coming up.
So mainly what we wanted to do is we want to shorten the function where we're sending that value.
So instead of ad style, we're going to say play it.
So that's going to be the function and we're going to pass over the value of lower animal.
And of course, you can shorten these as well.
You can shorten the for loop and you can even shorten the function syntax to the newer format with the
arrows.
But I'm not going to do that.
I'm just going to simply shorten the output within the function.
So instead of having two functions where I'm adding style and playing a sound, I'm going to copy ad
style and I'm going to update this with the playing of the sound, and I'm going to compress it all
into one simple function called Play It.
So still passing over that name.
We're grabbing that active element according to the name.
I can get rid of the console message.
And over here we had a switch statement where we were switching between the sounds.
Well, we don't have to do that anymore where we can take this.
And because we are using the same values that are being set out here instead of sound one and play,
we've got the value of the sound within name so we can break out of their user name and break Brek back
into that string value.
So let's go ahead and try that out with our shortened, more condensed function format.
Refresh the page, and now when we click the buttons, we see that it's going red as well as playing
the appropriate sound.
So go ahead and try this one out for yourself, shorten the code, and you can also apply different
ways to shorten the code as well.