JSFiddle - React, Tailwind, and code Playground

by babich_ss

HTML

<p id="output"></p>

JavaScript

"use strict";
const arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
const output = document.getElementById("output");
// Oneliner to obtain random value from range. This is an arrow function
const getIndex = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

// Rolling random values till we get one not used before
const generateUniqueIndex = (storage, min, max) => {
	var index;
	while (index === undefined || storage.includes(index)) {
		index = getIndex(min, max);
	}
	return index;
};

function printRandom (arr) {
	const indexes = []; //Array to store used indexes
	const [min, max] = [0, arr.length - 1];

	// Iterating with random indexes but from 0 to array length
	for (let i = 0; i <= max; i++) {
		let index = generateUniqueIndex(indexes, min, max);
		output.innerText += arr[index]; // Do some action with active index
		indexes.push(index); // Save used index
	}
}

printRandom(arr);