How to create a dynamic URL for audio files using a for loop? Javascript

Infinite-zero

I started learning to code recently and I'm having a problem with this self-challenge. My intent is to use a for-loop to cycle through the array containing color names, which represent buttons, to create a dynamic URL to audio files instead of individually adding the URL for the audio file to each button. Being a newbie, this code makes sense to me, but it isn't working. Using the browser "inspect" tool, I noticed the output for buttonColours[i] is coming out as undefined. Please help me understand why!!!

var buttonColours = ["red","blue","green","yellow"];
var sounds = [];

for (var i = 0; i < buttonColours.length; i++){

    $("."+buttonColours[i]+"").click(function(event){

        // Add sound when button is clicked
        sounds[i]= new Audio("sounds/"+buttonColours[i]+".mp3");
        sounds[i].play();

        // Add CSS class when button is clicked
        $(".red").addClass("pressed");
        setTimeout(function(){
            $(".red").removeClass("pressed");
        }, 200);
    });
}
Emiel Zuurbier

In addition to right answer of Sachin Nayak, another way to solve this issue is to use let instead of var. let creates a variable inside the scope of the loop where var will be a global variable that will be overwritten at every iteration. That means that with let you don't overwrite the value, but create a new one at each iteration, thus the reference stays intact.

var buttonColours = ["red","blue","green","yellow"];
var sounds = [];

// Notice let i instead of var i.
for (let i = 0; i < buttonColours.length; i++) {

  $("." + buttonColours[i]).click(function(event) {

    // Add sound when button is clicked
    sounds[i] = new Audio("sounds/" + buttonColours[i] + ".mp3");
    sounds[i].play();

    // Add CSS class when button is clicked
    $(".red").addClass("pressed");
    setTimeout(function() {
      $(".red").removeClass("pressed");
    }, 200);
  });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to loop through multiple audio files and play them sequentially using javascript?

From Dev

How to loop through multiple audio files and play them sequentially using javascript?

From Dev

How to create an audio player using html and javascript

From Dev

How to create grid of UIImageView dynamic using for loop?

From Dev

How do I loop through a javascript array of audio files?

From Dev

How to create a dynamic object using Javascript?

From Dev

How to create multiple textbox in javascript using for loop?

From Dev

How to create dynamic number of TextBox controls using loop?

From Dev

How to create dynamic URL without using anchor href?

From Dev

Create a audio object by using JavaScript code

From Java

Why does using cloneNode() to create multiple audio files stop me from controlling attributes of the audio source as and how can I override this

From Dev

How to avoid audio loop lag in javascript?

From Dev

Create a dynamic nested dictionary in python using for loop

From Dev

Create a dynamic nested dictionary in python using for loop

From Dev

How to convert bitrate of audio files in javascript?

From Dev

How to create a dynamic javascript object

From Dev

How to create dynamic regex in javascript

From Dev

Create Table in Javascript using For Loop

From Dev

How to create a infinite loop of Shuffle Text Effect using JavaScript?

From Dev

How do you create a for loop using a variable in javascript?

From Dev

how to create a table using pure javascript that will loop data into it

From Dev

Loop for recognizing all files in a folder using Dejavu library for audio fingerprinting

From Dev

How to create a download button for multiple files using Liferay and JavaScript?

From Dev

how to get soundcloud audio download url by using audio file id

From Dev

How to loop and create a dynamic bootstrap grid

From Dev

How to create dynamic IDs from a foreach loop

From Dev

Javascript: How to loop through files

From Dev

Javascript: How to loop through files

From Dev

How to create an image array from a URL array in JavaScript using map?

Related Related

  1. 1

    How to loop through multiple audio files and play them sequentially using javascript?

  2. 2

    How to loop through multiple audio files and play them sequentially using javascript?

  3. 3

    How to create an audio player using html and javascript

  4. 4

    How to create grid of UIImageView dynamic using for loop?

  5. 5

    How do I loop through a javascript array of audio files?

  6. 6

    How to create a dynamic object using Javascript?

  7. 7

    How to create multiple textbox in javascript using for loop?

  8. 8

    How to create dynamic number of TextBox controls using loop?

  9. 9

    How to create dynamic URL without using anchor href?

  10. 10

    Create a audio object by using JavaScript code

  11. 11

    Why does using cloneNode() to create multiple audio files stop me from controlling attributes of the audio source as and how can I override this

  12. 12

    How to avoid audio loop lag in javascript?

  13. 13

    Create a dynamic nested dictionary in python using for loop

  14. 14

    Create a dynamic nested dictionary in python using for loop

  15. 15

    How to convert bitrate of audio files in javascript?

  16. 16

    How to create a dynamic javascript object

  17. 17

    How to create dynamic regex in javascript

  18. 18

    Create Table in Javascript using For Loop

  19. 19

    How to create a infinite loop of Shuffle Text Effect using JavaScript?

  20. 20

    How do you create a for loop using a variable in javascript?

  21. 21

    how to create a table using pure javascript that will loop data into it

  22. 22

    Loop for recognizing all files in a folder using Dejavu library for audio fingerprinting

  23. 23

    How to create a download button for multiple files using Liferay and JavaScript?

  24. 24

    how to get soundcloud audio download url by using audio file id

  25. 25

    How to loop and create a dynamic bootstrap grid

  26. 26

    How to create dynamic IDs from a foreach loop

  27. 27

    Javascript: How to loop through files

  28. 28

    Javascript: How to loop through files

  29. 29

    How to create an image array from a URL array in JavaScript using map?

HotTag

Archive