I'm completely new to JavaScript. From the code below, what is the proper way to make sure that the background colors doesn't repeat themselves when clicked?
Add: I need the next color not to be the previous color because if so you can't know if the page works properly when clicked. It's okay if the whole array repeat itself randomly so long as the same color isn't repeated twice subsequently e.g. red and then red again.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>01</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row vh-100 d-flex align-items-center">
<div class="col text-center">
<button>Click Me!</button>
</div>
</div>
</div>
</div>
<script>
const button = document.querySelector('button')
const body = document.querySelector('body')
const colors = ['red', 'green', 'blue', 'yellow', 'pink', 'purple']
body.style.backgroundColor = 'violet'
button.addEventListener('click', changeBackground)
function changeBackground(){
const colorIndex = parseInt(Math.random()*colors.length)
body.style.backgroundColor = colors[colorIndex]
}
</script>
</body>
</html>