So I have this script that randomises through a passed object. It works fine.
But I added a feature that checks if the song that was played before is the same as the song that will be played in the function, and if so, making it not the same. This doesn't work.
I don't know why, but for some reason, the same songs can play after each other and it's pretty annoying.
Here's the part of the script that randomizes:
const musicSelect = function(selectedObject) {
const rand = Math.floor(Math.random() * selectedObject.length);
if (selectedObject[rand].url != playingSong) {
source.src = selectedObject[rand].url;
playingSong = source.src;
console.log("Playing " + selectedObject[rand].name);
document.getElementById("Title").innerHTML = "Playing: " + selectedObject[rand].name;
/* Making it that its impossible to get the same music consecutive times */
} else {
/* Check if the song is the last song in the object,
if so then pick a new song another way to prevent error */
if (selectedObject[rand] == selectedObject.length) {
source.src = selectedObject[rand - 1].url;
playingSong = source.src;
console.log("Playing " + selectedObject[rand - 1]);
document.getElementById("Title").innerHTML = "Playing: " + selectedObject[rand - 1];
} else {
source.src = selectedObject[rand + 1].url;
playingSong = source.src;
console.log("Playing " + selectedObject[rand + 1]);
document.getElementById("Title").innerHTML = "Playing: " + selectedObject[rand + 1];
}
}
music.load();
music.play();
};