JavaScript How can I shorten my code with for() or something else in JavaScript? short code with,short code with twilio,shorten code in

WHAT I WANT TO DO

I want to shorten my code. This Drum Play App plays sound by pressing certain keys or clicking with your mouse.

It works, but the code for click events is too long because I repeated the same code many times.

Could anyone make it cleaner/shorter?

WHAT I TRIED

I tried for loop, like below:

document.querySelector(`div[data-key="${dataKeys[i]}"]`).addEventListener...

But it didn't work.

MY CURRENT CODE

Here is my code.

<body>

  <div class="keys">
    <div data-key="65" class="key">
      <kbd>A</kbd>
      <span class="sound">clap</span>
    </div>
    <div data-key="83" class="key">
      <kbd>S</kbd>
      <span class="sound">hihat</span>
    </div>
    <div data-key="68" class="key">
      <kbd>D</kbd>
      <span class="sound">kick</span>
    </div>
    ...

  </div>

  <audio data-key="65" src="sounds/clap.wav"></audio>
  <audio data-key="83" src="sounds/hihat.wav"></audio>
  <audio data-key="68" src="sounds/kick.wav"></audio>
  ...

<script>
  window.addEventListener('keydown', function(e) {
    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
    if (!audio) return; // Stop the function from running all together
    audio.currentTime = 0; // Rewind to the start
    audio.play();
  });

  document.querySelector('div[data-key="65"]').addEventListener('click', function(e) {
    console.log('I clicked.')
    const clickedAudio = document.querySelector(`audio[data-key="65"]`);
    clickedAudio.currentTime = 0; // Rewind to the start
    clickedAudio.play();
  });

  document.querySelector('div[data-key="83"]').addEventListener('click', function(e) {
    console.log('I clicked.')
    const clickedAudio = document.querySelector(`audio[data-key="83"]`);
    clickedAudio.currentTime = 0; // Rewind to the start
    clickedAudio.play();
  });

  document.querySelector('div[data-key="68"]').addEventListener('click', function(e) {
    console.log('I clicked.')
    const clickedAudio = document.querySelector(`audio[data-key="68"]`);
    clickedAudio.currentTime = 0; // Rewind to the start
    clickedAudio.play();
  });
  ...

</script>

</body>

I'd appreciate it if anyone could make my code cleaner or shorter.

Answer:1

I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...

I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...

  1. how to include duplicate values in pivot table
  2. how to include duplicate values in sql

Problem Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right? [1, 2, 3].sort((a, b) => { if (a === 2) return -1; return 0; }); Chrome ...

Problem Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right? [1, 2, 3].sort((a, b) => { if (a === 2) return -1; return 0; }); Chrome ...

I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...

I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...

  1. javascript multiple event listeners
  2. javascript multiple event handlers
  3. javascript multiple event listeners for the same event
  4. javascript multiple event listeners order
  5. javascript multiple event listeners same function
  6. javascript multiple event
  7. javascript multiple event handlers onclick
  8. javascript multiple event binding
  9. javascript event multiple functions
  10. javascript event multiple elements
  11. javascript event multiple keys
  12. javascript event multiple buttons
  13. javascript add multiple event listeners
  14. javascript prevent multiple event listeners
  15. javascript attach multiple event handlers
  16. javascript adding multiple event listeners
  17. javascript register multiple event handlers
  18. javascript remove multiple event listeners
  19. javascript add multiple event handlers
  20. javascript bind multiple event handlers

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...