Hi,
I am new to web development so I am still kind of unsure to how everything works so sorry if it is something obvious.
To create the scenario, I need to create a music player using html5, javascript and css. I very much got stuck on how to shuffle the songs that I have so that a random order is created and the previously played songs dont play after.
I have used a checkbox to create the shuffle 'button' which looks like this in html:
<input type="checkbox" value="None" id="random_song" onclick="shuffleSong()" name="check" />
<label class="random" for="random_song"></label>
and then I have created the following code in javascript to shuffle:
function shuffleSong() {
const song = document.querySelector('#song')
var shuffle = document.getElementById('random_song');
var y = 0;
song.addEventListener('ended', function () {
if (shuffle.checked) {
var x = Math.floor((Math.random() * 7));
alert(x); // this is just for testing so I know which array element it will play
song.src = songs[x];
thumbnail.src = thumbnails[x];
background.src = thumbnails[x];
songArtist.innerHTML = songArtists[x];
songTitle.innerHTML = songTitles[x];
playing = true;
playPause();;
} else { //this works perfectly
y++;
if (y > 6) {
y = 0;
};
song.src = songs[y];
thumbnail.src = thumbnails[y];
background.src = thumbnails[y];
songArtist.innerHTML = songArtists[y];
songTitle.innerHTML = songTitles[y];
playing = true;
playPause();
}
});
}
The following are arrays: song, thumbnail, background, songArtist and songTitle which of course contain all the relevant information for the songs that will be played.
The playPause() is a function to just automatically start playing the song as well if the play button is on.
Is there anyone who could tell me what I need to add to the code to eliminate the previously created random number?
Thank you so much in advance :)