Hello,
The aim here is to capitalize only the first letter of each substring, this code seems to run correctly in my terminal (i’m using node to run it), but it does not pass the FCC tests,
Could you please review and give me a hint on what’s wrong ?
Thank you,
function titleCase(str) {
let arr = str.split(' ');
let arrayOfChars = [];
let capitalized = [];
for (let i = 0; i < arr.length; i++) {
arrayOfChars.push(arr[i].split(''));
}
for (let i = 0; i < arrayOfChars.length; i++) {
for (let j = 0; j < arrayOfChars[i].length; j++) {
if (j == 0) { capitalized.push(arrayOfChars[i][j].toUpperCase()); }
else { capitalized.push(arrayOfChars[i][j].toLowerCase()); }
}
if (i != arrayOfChars.length) { capitalized.push(' '); }
}
return capitalized.join('');
}
I finally figured it out, the final condition should be
if (i != arrayOfChars.length - 1) { capitalized.push(' '); }
Reading my code out of my post helped see the problem