I’m creating demonstration code for a presentation.
var fruitList = ["apple", "banana", "cherry", "durian", "elderberry", "fig", "grape", "huckleberry", "incaberry", "juniper", "kapok", "lime", "mango", "nectarine", "olive", "plum", "quince", "rambutan", "strawberry", "tangerine", "ugli", "vanilla", "watermelon", "xigua", "yarrow", "zhe"];
for (var i = 0; i < fruitList.length; i++) {
if (fruitList[i].charAt(fruitList[i].length-1) == "y") {
fruitList.splice(i, 1);
}
}
console.log(fruitList);
This code is working almost perfectly, but there’s one strange problem: the incaberry. No matter how many times I rename it, or put another element before or after it, it never gets removed from the list.
I tried to introduce new elements to the list, hoping to discover it was some strange problem with indexing, but it still stayed in the list.
Through experimentation, I learned that for any set of consecutive elements ending in y, only the first will be removed. How do I fix this?