I'm looking to create a function that can convert characters in a string at even indices to uppercase, while ignoring spaces as even indices.
For example: 'This is a test' should become 'ThIs Is A TeSt'
My initial solution didn't work as expected because it wasn't properly handling the spaces when identifying even indices.
function capitalizeEvenIndices(string) {
return string.split("").map((char, index) => index % 2 === 0 && char !== " " ? char.toUpperCase() : char).join("");
}
On my second attempt, I faced an issue where the string elements were not being converted to uppercase. Any guidance on this problem would be greatly appreciated as the function currently returns the original string unchanged.
function capitalizeEvenIndices(string) {
let indexCount = 0;
let isSpace = false;
for (let i = 0; i < string.length; i++) {
if (string[i] === " ") {
isSpace = true;
}
if (indexCount % 2 === 0 && !isSpace) {
string[indexCount] = string[indexCount].toUpperCase();
}
indexCount++;
isSpace = false;
}
return string;
}