I have an array of colors and then I randomized them. I want to assign different color to each list item instead of having items with the same colors or having repeated colors.
const myList = document.querySelector('.myList');
const listItems = myList.querySelectorAll('li');
const itemColors = ['red', 'green', 'royalblue', 'purple','cornflowerblue', 'sienna','palevioletred','orange'];
listItems.forEach((item, index) => {
let randomColors = itemColors[Math.floor(Math.random() * itemColors.length)];
item.style.color = randomColors;
});
<ul class="myList">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
<li>Soup</li>
<li>Juice</li>
<li>Beer</li>
<li>Wine</li>
<li>Soda</li>
</ul>