[Solved] How to select every 2nd element of an array in a for loop?


Use modulus(%) operator to get every 2nd element in loop and add fontStyle on it:

var myList = document.getElementById('myList');
var addList = ["Python", "C", "C++", "Ruby", "PHP", "Javascript", "Go", "ASP", "R"];

for (var i = 0; i < addList.length; i++) {
  var newLi = document.createElement("li");
  newLi.innerHTML = addList[i];
  if(i%2==0){
  newLi.style.fontStyle = "italic"
  newLi.innerHTML = "<strong>"+addList[i]+"</strong>";
  }


  
  myList.appendChild(newLi);
}
<ul id="myList"></ul>

2

solved How to select every 2nd element of an array in a for loop?