<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
body {
  display: flex;
  flex-wrap: wrap;
}

.item {
  width: 100px;
  height: 100px;
  margin: 5px;
}
const colors = ['black', 'yellow', 'red', 'green']

const items = document.querySelectorAll('.item')

items.forEach((el, i) => {
  if(i <= 2) {
    el.style.background = colors[i]
  } else {
    el.style.background = colors[3]
  }
})

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.