+ 1

How can randomize or shuffle data in JavaScript?

Hi there, I want to randomize or shuffle the list of movie names and always want to come up with completely unique movie names. For example, this is my list of movie names: 1. The Shawshank Redemption 2. The Godfather 3. Fight Club 4. Inception 5. Goodfellas 6. Seven Samurai 7. The Matrix Actually, I am want to create a similar type of website for my own project. Here is a sample site: https://randomoutputs.com/random-movie-generator Please kindly help me, with an easy and understandable way, I am relatively new to javascript. Thanks

20th Mar 2022, 10:01 PM
Maryan Kortlever
1 Answer
+ 1
function getRandomMovies(movies) { const length = movies.length; const index = Math.floor(Math.random()*length); return movies[index]; } const movieList = [ "The Shawshank Redemption", "The Godfather", "Fight Club", "Inception", "Goodfellas", "Seven Samurai", "The Matrix" ]; const movie = getRandomMovies(movieList); console.log("Random movie title: " + movie); https://code.sololearn.com/W4HagYj7DH4Q/?ref=app
21st Mar 2022, 11:41 AM
CalviŐČ
CalviŐČ - avatar