let colorOptArr = [
  document.getElementsByClassName("bx1"),
  document.getElementsByClassName("bx2"),
  document.getElementsByClassName("bx3"),
  document.getElementsByClassName("bx4")
];
for (let i = 1; i <= 4; i++) {
  colorOptArr[i].style.backgroundColor = `red`;
}<section class="opt">
  <div class="colors bx1"></div>
  <div class="colors bx2"></div>
  <div class="colors bx3"></div>
  <div class="colors bx4"></div>
</section>Why this code is not changing the color of the div and giving the error that can't change background of undefined value
 
    