How use for loop to remove elements from array?

So far I manage to figure out how to remove a image from array using removeChild(list.children[0]) , I been trying to use pop to remove the last element in the array using the for loop without success;but I would like to know how to implement a for loop on my second function to remove the last picture in the array rather than use removeChild(list.children[0]).


CODEPEN

I would stick to what you have already to be honest.

just swap the [0] out in the last line:

list.removeChild(list.children[list.children.length-1]);

Iā€™m not really sure what you goal is. Are you trying to add them in groups from the myArray images? like 2+ images at a time? Then trying to remove them in groups too?

var myArray = ['3.jpg','4.jpg']; //move outside `addCars` scope so `removeCars` can read it too

function addCars() {
    var carImgContainer = document.getElementById("carImgContainer");
    for (var i = 0, p = myArray.length; i < p; i++) {
    var img = document.createElement("img");
    img.src = myArray[i];
    img.className = "car-image-style";
    carImgContainer.appendChild(img);
    };
}
function removeCars() {
  var list = document.getElementById("carImgContainer");
  for(let i = myArray.length; 0 < i; i--){
    list.removeChild(list.children[list.children.length-1]);
  };
}
1 Like

thank you so much, your help was pretty handy