
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Remove random item from array and then remove it from array until array is empty
We are given an array of string / number literals. We are required to create a function removeRandom() that takes in the array and recursively removes one random item from the array and simultaneously printing it until the array contains items.
This can be done through creating a random number using Math.random() and removing the item at that index using Array.prototype.splice() and printing it until the length of array shrinks to 0.
Here is the code for doing the same −
Example
const arr = ['Arsenal', 'Manchester United', 'Chelsea', 'Liverpool', 'Leicester City', 'Manchester City', 'Everton', 'Fulham', 'Cardiff City']; const removeRandom = (array) => { while(array.length){ const random = Math.floor(Math.random() * array.length); const el = array.splice(random, 1)[0]; console.log(el); } }; removeRandom(arr);
The output in console can be −
Note − As it is a random output, it is likely to differ every time, so this is just one of many possible outputs.
Output
Leicester City Fulham Everton Chelsea Manchester City Liverpool Cardiff City Arsenal Manchester United
- Related Articles
- MongoDB query to remove item from array?
- How to remove an item from JavaScript array by value?
- Remove item from a nested array by indices in JavaScript
- How can I remove a specific item from an array JavaScript?
- Remove element from array referencing spreaded array in JavaScript
- How can I remove a specific item from an array in JavaScript
- Remove/ filter duplicate records from array - JavaScript?
- Remove '0','undefined' and empty values from an array in JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- How to remove elements from an array until the passed function returns true in JavaScript?
- PHP: Remove object from array
- Remove duplicates from a array of objects JavaScript
- Python program to Remove and print every third from list until it becomes empty?
- Remove elements from array in JavaScript using includes() and splice()?
- Remove number from array and shift the remaining ones JavaScript

Advertisements