Because when you delete, the next index moves down one. Loop in the opposite direction.
for (var i = this.length - 1; i >= 0; i--) { ... }
Using a while loop might bring more into it
Object.prototype.removeClass = function(class_name) {
if (this.length) {
while(this.length>0) {
console.log(this.length);
this[0].classList.remove(class_name);
}
}
}
document.getElementsByClassName("test").removeClass("test");
The output would be 7,6,5,4,3,2,1
So this
is updated when you remove the class