title: How to change a variable audio file value when calling a function to play the audiofile in Vanilla JavaScript?
body: <p>I am working on a project that when I used a specific keyboard's key a different sound must be played. I have a created a function to play the sound, then I attached an <strong>Event Listener to the DOM</strong> &amp; finally I checked which key has been pressed &amp; play the sound who correspond with the key.</p>
  
  <p><strong>I am trying to do this by writing DRY code.</strong> How can I change variable sound value when calling the function to play the sound file?</p>
  
  <p>I have a tried changing the <code>var sound = New Audio('filepath');</code> before calling the function <code>playSound()</code> but it doesn't work.</p>
  
  <pre><code>var sound = new Audio('assets/sounds/clap.wav');
  // function to play sounds
  function playSound() {
    sound.play();
  }
  
  // adds event listener &amp; checks keyboard to play different sounds
  document.addEventListener('keydown', function(event){
    if(event.keyCode == 65){
      playSound();
    } else if ( event.keyCode == 83 ) {
      playSound();
    }
    else if( event.keyCode == 68 ) {
      playSound();
    }
    else if( event.keyCode == 70 ) {
      playSound();
    }
  });
  </code></pre>