0
\$\begingroup\$

I have this script and I want to ask if it's good/optimal and if it's not how should I change it?

What it's doing is when the website loads music, it starts to play when the user clicks prevent play() not running because user didn't interact) from an array of music and when a music stops a new one plays.

In the future I will have many songs in the array. I thought maybe I could change it so it selects from a folder (like a physhical folder in Windows) is this possible?

console.logs for debugging

var music = document.getElementById("BackgroundM");
var source = document.getElementById("source");
var musicArray = [
    "/music/Cruel.mp3",
    "/music/Ragnarok.mp3",
    "/music/Berserk.mp3",
    "/music/Wrath.mp3",
    "/music/Immaculate.mp3",
    "/music/BassSlut.mp3"
]
    
music.volume = 0.05;

var musicSelect = function() {
    if(music.paused) {
        var rand = Math.floor(Math.random() * musicArray.length)
        musicArray[rand]
        source.src = musicArray[rand]
        music.load();
        music.play();
        console.log("Playing " + source.src)
    }
}

document.addEventListener('click', function() {
    console.log(music.paused)
    musicSelect()
})

music.addEventListener('pause', function(){
    console.log("Music is paused")
    musicSelect()
})

\$\endgroup\$
3
  • 4
    \$\begingroup\$ Do you have the HTML? There's not much code here. Also, we can't advise on adding new features. That's outside the scope of a code review. Thanks. \$\endgroup\$ Commented Oct 15, 2022 at 15:17
  • \$\begingroup\$ Everything is globally scoped and accessed in the function, the code is optimal but not ideal in terms of design patterns, this style of code becomes unmaintainable. \$\endgroup\$ Commented Oct 15, 2022 at 16:40
  • 1
    \$\begingroup\$ @ericraio You have a pretty good start to an answer. Say a little more about why it becomes unmaintainable. This question can be answered. \$\endgroup\$ Commented Oct 16, 2022 at 14:06

1 Answer 1

4
\$\begingroup\$

I saw that you were advised to bring this question over here from Stack Overflow and I think I understand what you are asking: Is the code optimal and can it be improved i.e. made faster due to the concern that you will have a much bigger music selection than in the current array.

What you are currently doing is:

  • storing the file paths of music tracks in an array (to use as the music src)
  • creating a random number within the limits of the array length
  • selecting the music track using that random number as the index of the array

As it is, that is pretty 'optimal' arguably, it may be quicker to access data from an object literal musicStore = {0: "/music/Cruel.mp3"} but, unless you have a phenomenal amount of entries in the array / store then the speed difference will be unnoticeable.

As for your future aspirations for this app and the possibilities of selecting a random file from a directory (if this was what you meant) then no! It is not possible. JavaScript has no access to the file system on the client or server. The closest you can get to the file system is through an HTML input type="file" element which gives the user the ability to select a file, or selection of files from their local HDD, which will not achieve what you want.

\$\endgroup\$
5
  • \$\begingroup\$ Yes this is what i've meant exactly. I don't know whats object literal, I will look it up! Thanks for the help! \$\endgroup\$ Commented Oct 15, 2022 at 17:46
  • \$\begingroup\$ An object literal is like an array but with curly braces { } instead of square brackets [ ] as an array is defined. An object's contents are defined with key: "value" pairs, so, to put your array into an object I simply defined the first key as 0 with a value of "/music/Cruel.mp3" to add an additional entry, put a comma after the first and then add the second entry: musicStore = {0: "/music/Cruel.mp3", 1: "/music/Ragnarok.mp3" } etc. etc. What is really wonderful about objects is that the value of the key: "value" pair can be any valid JavaScript object! Happy Coding :-) \$\endgroup\$ Commented Oct 15, 2022 at 20:18
  • \$\begingroup\$ oh so like dictionaries \$\endgroup\$ Commented Oct 15, 2022 at 20:27
  • \$\begingroup\$ JavaScript doesn't have a native "dictionary" data type, but they are quite similar and work alike. \$\endgroup\$ Commented Oct 15, 2022 at 20:37
  • \$\begingroup\$ Aight i made it with object and it works fine! I gave all the musics a url key and a name key :) thanks \$\endgroup\$ Commented Oct 16, 2022 at 12:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.