I have an array of elements, ["apple", "cherry", "raspberry", "banana", "pomegranate"], and I want it so that every odd element is capitalized: ["Apple", "cherry", "Raspberry", "banana", "Pomegranate"].
I can capitalize every element in the array, and I can filter out every odd element, but not at the same time (i.e. filtering only shows the odd elements).
Does anyone have any approaches and/or recommendations for this? I've seen questions about capitalizing every other letter, retrieving every other array element, etc., but nothing like what I've asked (yet, I'm still looking).
function alts(arr) {
const newArr = arr.filter((el, idx) => {
if (idx % 2 === 0) {
return arr.map(a => a.charAt(0).toUpperCase() + a.substr(1));
}
})
return newArr;
}
console.log(alts(["apple", "cherry", "raspberry", "banana", "pomegranate"]));
// Just returns [ 'apple', 'raspberry', 'pomegranate' ]