I need help with returning a new value to an array after using array.splice()

Hello all! I am working on a small ‘project’ that I thought of while driving and listening to music. This project is called Music Shuffle. Just like how the music apps work, when you’re listening to music on shuffle, you will not get repeated songs. So in the most basic form, I have assigned a set value of likedSongs (10 songs) that will then be created into a list in an array (1-10). I then want to randomly shuffle and get assigned a number. This number will then be removed from the array of likedSongs, and then update the array to not include that number anymore. The next time I select a random number, there will be no possible way to get a repeat and this will continue till there are no more possible elements in the array left.

//the amount of songs that the user has
let likedSongs = 10;

//this makes an array from the length of likedSongs
let listOfLikedSongs = Array.from(
  { length: likedSongs },
  (_, index) => index + 1
);

//this choses a random element from the array
const randomLikedSong =
  listOfLikedSongs[Math.floor(Math.random() * listOfLikedSongs.length)];

//the index in the array that equals the value of randomLikedSong
const index = listOfLikedSongs.indexOf(randomLikedSong);

//lets us use the index to splice out the element from the array
if (index > -1) {
  listOfLikedSongs.splice(index, 1);

} //i want to update the array that lists the new updated element list



//this outputs the randomlikedsong variable //helps me test the code
console.log(randomLikedSong);
console.log(index);
console.log(listOfLikedSongs);

//the amount of songs that the user has let likedSongs = 10; //this makes an array from the length of likedSongs let listOfLikedSongs = Array.from( { length: likedSongs }, (_, index) => index + 1 ); //this choses a random element from the array const randomLikedSong = listOfLikedSongs[Math.floor(Math.random() * listOfLikedSongs.length)]; //the index in the array that equals the value of randomLikedSong const index = listOfLikedSongs.indexOf(randomLikedSong); //lets us use the index to splice out the element from the array if (index > -1) { listOfLikedSongs.splice(index, 1); } //i want to update the array that lists the new updated element list //this outputs the randomlikedsong variable //helps me test the code console.log(randomLikedSong); console.log(index); console.log(listOfLikedSongs);

This is how far I have gotten. This code so far shuffles a random song number and splices it out of an array. The issue I am having now is that I do not know how to update the array to where it remembers that the previous selected number is not in roation anymore.

I know it will deal with return but putting it into code is what I am struggling with. I tried functions but it seems like I didn’t put things together correctly. Also, I know there is probably a more concise and easier way to code this but I am a beginner level right now, so I would appreciate if the solution can fit into my vocabulary :smiley:

If anyone can help that would be great!

Carlos,

Are you familiar with functions yet? If so, then you need to turn randomLikedSong into a function that returns the song you want to play next. Then you need to build a function that calls the piece of code to splice the array that contains the remaining songs in the playlist. That should get you closer to where you want to be?

hello!

So I know the basics of functions but sometimes get confused about grammar.

let likedSongs = 10; let listOfLikedSongs = Array.from( { length: likedSongs }, (_, index) => index + 1 ); const arrayTotalElement = listOfLikedSongs.length; function randomLikedSong() { const randomSong = listOfLikedSongs[Math.floor(Math.random() * listOfLikedSongs.length)]; console.log(randomSong + ' randomLikedSong'); } function splice() { const index = listOfLikedSongs.indexOf(randomLikedSong()); console.log(index + ' this should be the index number of the randomLikedSong'); } splice();

This is what I have so far, and I’m having trouble getting the correct index that corresponds with the random number in the array that was given so I can splice it. I am trying to find indexOf the randomLikedSong function because that is what gives me the random number. Any ideas on the best way to complete that?

Thanks!

You are very nearly there! One suggestion is to make the name of your function a little clearer as to its purpose. For your random index, you need to return the value from the function. This will allow the random value to be used elsewhere. Checkout the below code and let me know if you have further questions on it.

let likedSongs = 10; let listOfLikedSongs = Array.from( { length: likedSongs }, (_, index) => index + 1 ); const arrayTotalElement = listOfLikedSongs.length; function randomLikedSong() { const randomSong = listOfLikedSongs[Math.floor(Math.random() * listOfLikedSongs.length)]; console.log(randomSong + ' randomLikedSong'); return randomSong; } function removeSongFromPlaylist() { const index = listOfLikedSongs.indexOf(randomLikedSong()); console.log(index + ' this should be the index number of the randomLikedSong'); listOfLikedSongs.splice(index, 1); console.log(listOfLikedSongs) } removeSongFromPlaylist(); removeSongFromPlaylist();